libuhttpd: New callback: on_accept

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-23 23:36:26 +08:00
parent f21e2001e1
commit ddf6e8dd86
4 changed files with 17 additions and 0 deletions

View File

@ -24,6 +24,11 @@
#include <string.h>
#include <libubox/ulog.h>
static void on_accept(struct uh_client *cl)
{
ULOG_INFO("New connection from: %s:%d\n", cl->get_peer_addr(cl), cl->get_peer_port(cl));
}
static int on_request(struct uh_client *cl)
{
const char *path = cl->get_path(cl);
@ -103,6 +108,7 @@ int main(int argc, char **argv)
ULOG_INFO("Listen on: %s *:%d\n", srv->ssl ? "https" : "http", port);
srv->on_accept = on_accept;
srv->on_request = on_request;
uloop_run();

View File

@ -121,6 +121,11 @@ static inline const char *client_get_peer_addr(struct uh_client *cl)
return inet_ntoa(cl->peer_addr.sin_addr);
}
static inline int client_get_peer_port(struct uh_client *cl)
{
return ntohs(cl->peer_addr.sin_port);
}
static inline const char *client_get_url(struct uh_client *cl)
{
return kvlist_get(&cl->request.url, "url");
@ -625,6 +630,7 @@ void uh_accept_client(struct uh_server *srv, bool ssl)
cl->get_version = client_get_version;
cl->get_method = client_get_method;
cl->get_peer_addr = client_get_peer_addr;
cl->get_peer_port = client_get_peer_port;
cl->get_url = client_get_url;
cl->get_path = client_get_path;
cl->get_query = client_get_query;
@ -632,6 +638,9 @@ void uh_accept_client(struct uh_server *srv, bool ssl)
cl->get_header = client_get_header;
cl->get_body = client_get_body;
if (srv->on_accept)
srv->on_accept(cl);
return;
err:
close(sfd);

View File

@ -120,6 +120,7 @@ struct uh_client {
const char *(*get_method)(struct uh_client *cl);
const char *(*get_version)(struct uh_client *cl);
const char *(*get_peer_addr)(struct uh_client *cl);
int (*get_peer_port)(struct uh_client *cl);
const char *(*get_url)(struct uh_client *cl);
const char *(*get_path)(struct uh_client *cl);
const char *(*get_query)(struct uh_client *cl);

View File

@ -36,6 +36,7 @@ struct uh_server {
void (*set_index_file)(struct uh_server *srv, const char *index_file);
void (*on_error404)(struct uh_client *cl);
int (*on_request)(struct uh_client *cl);
void (*on_accept)(struct uh_client *cl);
#if (UHTTPD_SSL_SUPPORT)
int (*ssl_init)(struct uh_server *srv, const char *key, const char *crt);