Skip to content

Commit

Permalink
feat: conn->get_uri
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Aug 25, 2021
1 parent 9d78b65 commit 73da5b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/connection.c
Expand Up @@ -281,6 +281,18 @@ static const char *conn_get_method_str(struct uh_connection *conn)
/* data of the request field */
#define O2D(c, o) ((const char *)c->rb.data + o)

static struct uh_str conn_get_uri(struct uh_connection *conn)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
struct uh_request *req = &conni->req;
struct uh_str uri;

uri.p = O2D(conni, req->url.offset);
uri.len = req->url.length;

return uri;
}

static struct uh_str conn_get_path(struct uh_connection *conn)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
Expand Down Expand Up @@ -539,14 +551,13 @@ static int on_headers_complete(struct http_parser *parser)
struct uh_str path;
int port;

canonpath((char *)O2D(conn, req->url.offset), &req->url.length);

http_parser_parse_url(O2D(conn, req->url.offset), req->url.length, false, u);

path.p = O2D(conn, u->field_data[UF_PATH].off) + req->url.offset;
path.len = u->field_data[UF_PATH].len;

canonpath((char *)path.p, &path.len);
u->field_data[UF_PATH].len = path.len;

log_debug("%s %.*s from %s %d\n", http_method_str(parser->method), (int)path.len, path.p,
addr_str, (saddr2str(sa, addr_str, sizeof(addr_str), &port) ? port : 0));

Expand Down Expand Up @@ -976,6 +987,7 @@ static void conn_init_cb(struct uh_connection *conn)
conn->get_addr = conn_get_addr;
conn->get_method = conn_get_method;
conn->get_method_str = conn_get_method_str;
conn->get_uri = conn_get_uri;
conn->get_path = conn_get_path;
conn->get_query = conn_get_query;
conn->get_header = conn_get_header;
Expand Down
1 change: 1 addition & 0 deletions src/uhttpd.h
Expand Up @@ -72,6 +72,7 @@ struct uh_connection {
const struct sockaddr *(*get_addr)(struct uh_connection *conn); /* peer address */
enum http_method (*get_method)(struct uh_connection *conn);
const char *(*get_method_str)(struct uh_connection *conn);
struct uh_str (*get_uri)(struct uh_connection *conn);
struct uh_str (*get_path)(struct uh_connection *conn);
struct uh_str (*get_query)(struct uh_connection *conn);
struct uh_str (*get_header)(struct uh_connection *conn, const char *name);
Expand Down

0 comments on commit 73da5b1

Please sign in to comment.