parent
b4ab19016d
commit
af2c594e09
|
@ -10,11 +10,11 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
|
||||||
|
|
||||||
void route_test(struct uh_connection *con)
|
void route_test(struct uh_connection *con)
|
||||||
{
|
{
|
||||||
struct uh_value *url = uh_get_url(con);
|
struct uh_str *url = uh_get_url(con);
|
||||||
struct uh_value *path = uh_get_path(con);
|
struct uh_str *path = uh_get_path(con);
|
||||||
struct uh_value name = uh_get_var(con, "name");
|
struct uh_str name = uh_get_var(con, "name");
|
||||||
struct uh_value *header_host = uh_get_header(con, "Host");
|
struct uh_str *header_host = uh_get_header(con, "Host");
|
||||||
struct uh_value *header_ua = uh_get_header(con, "User-Agent");
|
struct uh_str *header_ua = uh_get_header(con, "User-Agent");
|
||||||
char unescaped_name[128];
|
char unescaped_name[128];
|
||||||
|
|
||||||
uh_send_head(con, HTTP_STATUS_OK, -1, NULL);
|
uh_send_head(con, HTTP_STATUS_OK, -1, NULL);
|
||||||
|
|
|
@ -84,6 +84,7 @@ install(
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/uhttp.h
|
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/uhttp.h
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/log.h
|
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/log.h
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/buf.h
|
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/buf.h
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/str.h
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/http_parser.h
|
${CMAKE_CURRENT_SOURCE_DIR}/include/uhttp/http_parser.h
|
||||||
DESTINATION
|
DESTINATION
|
||||||
include/uhttp
|
include/uhttp
|
||||||
|
|
|
@ -44,15 +44,15 @@ struct uh_server {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct uh_header {
|
struct uh_header {
|
||||||
struct uh_value field;
|
struct uh_str field;
|
||||||
struct uh_value value;
|
struct uh_str value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct uh_request {
|
struct uh_request {
|
||||||
struct uh_value url;
|
struct uh_str url;
|
||||||
struct uh_value path;
|
struct uh_str path;
|
||||||
struct uh_value query;
|
struct uh_str query;
|
||||||
struct uh_value body;
|
struct uh_str body;
|
||||||
int header_num;
|
int header_num;
|
||||||
struct uh_header header[UH_HEADER_NUM_LIMIT];
|
struct uh_header header[UH_HEADER_NUM_LIMIT];
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef _UHTTP_BASE_H
|
#ifndef _UHTTP_H
|
||||||
#define _UHTTP_BASE_H
|
#define _UHTTP_H
|
||||||
|
|
||||||
#include "uhttp/uhttp.h"
|
#include "uhttp/uhttp.h"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef _UHTTP_STR_H
|
||||||
|
#define _UHTTP_STR_H
|
||||||
|
|
||||||
|
struct uh_str {
|
||||||
|
const char *at;
|
||||||
|
size_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,20 +1,16 @@
|
||||||
#ifndef _UHTTP_H
|
#ifndef _UHTTP_UHTTP_H
|
||||||
#define _UHTTP_H
|
#define _UHTTP_UHTTP_H
|
||||||
|
|
||||||
#include <ev.h>
|
#include <ev.h>
|
||||||
#include "uhttp/config.h"
|
#include "uhttp/config.h"
|
||||||
#include "uhttp/log.h"
|
#include "uhttp/log.h"
|
||||||
#include "uhttp/buf.h"
|
#include "uhttp/buf.h"
|
||||||
|
#include "uhttp/str.h"
|
||||||
#include "uhttp/http_parser.h"
|
#include "uhttp/http_parser.h"
|
||||||
|
|
||||||
struct uh_server;
|
struct uh_server;
|
||||||
struct uh_connection;
|
struct uh_connection;
|
||||||
|
|
||||||
struct uh_value {
|
|
||||||
const char *at;
|
|
||||||
size_t len;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef void (*uh_route_handler_t)(struct uh_connection *con);
|
typedef void (*uh_route_handler_t)(struct uh_connection *con);
|
||||||
|
|
||||||
const char *uh_version();
|
const char *uh_version();
|
||||||
|
@ -84,11 +80,11 @@ int uh_printf_chunk(struct uh_connection *con, const char *fmt, ...);
|
||||||
/* sets a callback to be executed on a specific path */
|
/* sets a callback to be executed on a specific path */
|
||||||
int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_t cb);
|
int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_t cb);
|
||||||
|
|
||||||
struct uh_value *uh_get_url(struct uh_connection *con);
|
struct uh_str *uh_get_url(struct uh_connection *con);
|
||||||
struct uh_value *uh_get_path(struct uh_connection *con);
|
struct uh_str *uh_get_path(struct uh_connection *con);
|
||||||
struct uh_value *uh_get_query(struct uh_connection *con);
|
struct uh_str *uh_get_query(struct uh_connection *con);
|
||||||
struct uh_value uh_get_var(struct uh_connection *con, const char *name);
|
struct uh_str uh_get_var(struct uh_connection *con, const char *name);
|
||||||
struct uh_value *uh_get_header(struct uh_connection *con, const char *name);
|
struct uh_str *uh_get_header(struct uh_connection *con, const char *name);
|
||||||
|
|
||||||
/* Unescapes strings like '%7B1,%202,%203%7D' would become '{1, 2, 3}' */
|
/* Unescapes strings like '%7B1,%202,%203%7D' would become '{1, 2, 3}' */
|
||||||
int uh_unescape(const char *str, int len, char *out, int olen);
|
int uh_unescape(const char *str, int len, char *out, int olen);
|
||||||
|
|
20
src/uhttp.c
20
src/uhttp.c
|
@ -160,7 +160,7 @@ static int on_body(http_parser *parser, const char *at, size_t len)
|
||||||
|
|
||||||
|
|
||||||
/* Return 1 for equal */
|
/* Return 1 for equal */
|
||||||
static int uh_value_cmp(struct uh_value *uv, const char *str)
|
static int uh_str_cmp(struct uh_str *uv, const char *str)
|
||||||
{
|
{
|
||||||
if (uv->len != strlen(str))
|
if (uv->len != strlen(str))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -174,7 +174,7 @@ static int on_message_complete(http_parser *parser)
|
||||||
struct uh_route *r;
|
struct uh_route *r;
|
||||||
|
|
||||||
list_for_each_entry(r, &con->srv->routes, list) {
|
list_for_each_entry(r, &con->srv->routes, list) {
|
||||||
if (uh_value_cmp(&con->req.path, r->path)) {
|
if (uh_str_cmp(&con->req.path, r->path)) {
|
||||||
r->cb(con);
|
r->cb(con);
|
||||||
if (!(con->flags & UH_CON_CLOSE))
|
if (!(con->flags & UH_CON_CLOSE))
|
||||||
con->flags |= UH_CON_REUSE;
|
con->flags |= UH_CON_REUSE;
|
||||||
|
@ -547,17 +547,17 @@ int uh_register_route(struct uh_server *srv, const char *path, uh_route_handler_
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline struct uh_value *uh_get_url(struct uh_connection *con)
|
inline struct uh_str *uh_get_url(struct uh_connection *con)
|
||||||
{
|
{
|
||||||
return &con->req.url;
|
return &con->req.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline struct uh_value *uh_get_path(struct uh_connection *con)
|
inline struct uh_str *uh_get_path(struct uh_connection *con)
|
||||||
{
|
{
|
||||||
return &con->req.path;
|
return &con->req.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline struct uh_value *uh_get_query(struct uh_connection *con)
|
inline struct uh_str *uh_get_query(struct uh_connection *con)
|
||||||
{
|
{
|
||||||
return &con->req.query;
|
return &con->req.query;
|
||||||
}
|
}
|
||||||
|
@ -599,12 +599,12 @@ int uh_unescape(const char *str, int len, char *out, int olen)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct uh_value uh_get_var(struct uh_connection *con, const char *name)
|
struct uh_str uh_get_var(struct uh_connection *con, const char *name)
|
||||||
{
|
{
|
||||||
struct uh_value *query = &con->req.query;
|
struct uh_str *query = &con->req.query;
|
||||||
const char *pos = query->at, *tail = query->at + query->len - 1;
|
const char *pos = query->at, *tail = query->at + query->len - 1;
|
||||||
const char *p, *q;
|
const char *p, *q;
|
||||||
struct uh_value var = {.at = NULL, .len = 0};
|
struct uh_str var = {.at = NULL, .len = 0};
|
||||||
|
|
||||||
assert(con && name);
|
assert(con && name);
|
||||||
|
|
||||||
|
@ -649,13 +649,13 @@ struct uh_value uh_get_var(struct uh_connection *con, const char *name)
|
||||||
return var;
|
return var;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct uh_value *uh_get_header(struct uh_connection *con, const char *name)
|
struct uh_str *uh_get_header(struct uh_connection *con, const char *name)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct uh_header *header = con->req.header;
|
struct uh_header *header = con->req.header;
|
||||||
|
|
||||||
for (i = 0; i < con->req.header_num; i++) {
|
for (i = 0; i < con->req.header_num; i++) {
|
||||||
if (uh_value_cmp(&header[i].field, name))
|
if (uh_str_cmp(&header[i].field, name))
|
||||||
return &header[i].value;
|
return &header[i].value;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue