Commit 02098b19 authored by Sergey Lyubka's avatar Sergey Lyubka

API change: folded user_data into request_info, and introduced event-specific ev_data

parent c189553c
...@@ -72,6 +72,8 @@ class mg_request_info(ctypes.Structure): ...@@ -72,6 +72,8 @@ class mg_request_info(ctypes.Structure):
('is_ssl', ctypes.c_int), ('is_ssl', ctypes.c_int),
('num_headers', ctypes.c_int), ('num_headers', ctypes.c_int),
('http_headers', mg_header * 64), ('http_headers', mg_header * 64),
('user_data', ctypes.c_void_p),
('ev_data', ctypes.c_void_p),
] ]
...@@ -86,10 +88,6 @@ class Connection(object): ...@@ -86,10 +88,6 @@ class Connection(object):
self.m = mongoose self.m = mongoose
self.conn = ctypes.c_void_p(connection) self.conn = ctypes.c_void_p(connection)
self.info = self.m.dll.mg_get_request_info(self.conn).contents self.info = self.m.dll.mg_get_request_info(self.conn).contents
self.user_data = self.m.dll.mg_get_user_data(self.conn)
self.log_message = self.m.dll.mg_get_log_message(self.conn)
self.reply_status_code = self.m.dll.mg_get_reply_status_code(self.conn)
self.ssl_context = self.m.dll.mg_get_ssl_context(self.conn)
def get_header(self, name): def get_header(self, name):
val = self.m.dll.mg_get_header(self.conn, name) val = self.m.dll.mg_get_header(self.conn, name)
......
...@@ -221,7 +221,7 @@ static void init_server_name(void) { ...@@ -221,7 +221,7 @@ static void init_server_name(void) {
static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) { static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) {
if (ev == MG_EVENT_LOG) { if (ev == MG_EVENT_LOG) {
printf("%s\n", mg_get_log_message(conn)); printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
} }
// Returning NULL marks request as not handled, signalling mongoose to // Returning NULL marks request as not handled, signalling mongoose to
......
...@@ -632,8 +632,7 @@ const char *mg_version(void) { ...@@ -632,8 +632,7 @@ const char *mg_version(void) {
return MONGOOSE_VERSION; return MONGOOSE_VERSION;
} }
const struct mg_request_info * struct mg_request_info *mg_get_request_info(struct mg_connection *conn) {
mg_get_request_info(const struct mg_connection *conn) {
return &conn->request_info; return &conn->request_info;
} }
...@@ -4000,11 +3999,12 @@ static void handle_request(struct mg_connection *conn) { ...@@ -4000,11 +3999,12 @@ static void handle_request(struct mg_connection *conn) {
struct mgstat st; struct mgstat st;
if ((conn->request_info.query_string = strchr(ri->uri, '?')) != NULL) { if ((conn->request_info.query_string = strchr(ri->uri, '?')) != NULL) {
*conn->request_info.query_string++ = '\0'; * ((char *) conn->request_info.query_string++) = '\0';
} }
uri_len = (int) strlen(ri->uri); uri_len = (int) strlen(ri->uri);
url_decode(ri->uri, (size_t)uri_len, ri->uri, (size_t)(uri_len + 1), 0); url_decode(ri->uri, (size_t)uri_len, (char *) ri->uri,
remove_double_dots_and_double_slashes(ri->uri); (size_t) (uri_len + 1), 0);
remove_double_dots_and_double_slashes((char *) ri->uri);
stat_result = convert_uri_to_file_name(conn, path, sizeof(path), &st); stat_result = convert_uri_to_file_name(conn, path, sizeof(path), &st);
conn->throttle = set_throttle(conn->ctx->config[THROTTLE], conn->throttle = set_throttle(conn->ctx->config[THROTTLE],
get_remote_ip(conn), ri->uri); get_remote_ip(conn), ri->uri);
......
...@@ -34,35 +34,74 @@ struct mg_connection; // Handle for the individual connection ...@@ -34,35 +34,74 @@ struct mg_connection; // Handle for the individual connection
// This structure contains information about the HTTP request. // This structure contains information about the HTTP request.
struct mg_request_info { struct mg_request_info {
char *request_method; // "GET", "POST", etc const char *request_method; // "GET", "POST", etc
char *uri; // URL-decoded URI const char *uri; // URL-decoded URI
char *http_version; // E.g. "1.0", "1.1" const char *http_version; // E.g. "1.0", "1.1"
char *query_string; // URL part after '?' (not including '?') or NULL const char *query_string; // URL part after '?', not including '?', or NULL
char *remote_user; // Authenticated user, or NULL if no auth used const char *remote_user; // Authenticated user, or NULL if no auth used
long remote_ip; // Client's IP address long remote_ip; // Client's IP address
int remote_port; // Client's port int remote_port; // Client's port
int is_ssl; // 1 if SSL-ed, 0 if not int is_ssl; // 1 if SSL-ed, 0 if not
int num_headers; // Number of headers int num_headers; // Number of headers
struct mg_header { struct mg_header {
char *name; // HTTP header name const char *name; // HTTP header name
char *value; // HTTP header value const char *value; // HTTP header value
} http_headers[64]; // Maximum 64 headers } http_headers[64]; // Maximum 64 headers
void *user_data; // User data pointer passed to the mg_start()
void *ev_data; // Event-specific data pointer
}; };
// Various events on which user-defined function is called by Mongoose. // Various events on which user-defined callback function is called by Mongoose.
enum mg_event { enum mg_event {
MG_NEW_REQUEST, // New HTTP request has arrived from the client // New HTTP request has arrived from the client.
MG_REQUEST_COMPLETE, // Mongoose has finished handling the request // If callback returns non-NULL, Mongoose stops handling current request.
MG_HTTP_ERROR, // HTTP error must be returned to the client // ev_data contains NULL.
MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message MG_NEW_REQUEST,
MG_INIT_SSL, // SSL initialization, sent before certificate setup
MG_WEBSOCKET_CONNECT, // Sent on HTTP connect, before websocket handshake. // Mongoose has finished handling the request.
// If user callback returns NULL, then mongoose proceeds // Callback return value is ignored.
// with handshake, otherwise it closes the connection. // ev_data contains NULL.
MG_WEBSOCKET_READY, // Handshake has been successfully completed. MG_REQUEST_COMPLETE,
MG_WEBSOCKET_MESSAGE, // Incoming message from the client
MG_WEBSOCKET_CLOSE, // Client has closed the connection // HTTP error must be returned to the client.
// If callback returns non-NULL, Mongoose stops handling error.
// ev_data contains HTTP error code:
// int http_reply_status_code = (int) request_info->ev_data;
MG_HTTP_ERROR,
// Mongoose logs a message.
// If callback returns non-NULL, Mongoose stops handling that event.
// ev_data contains a message to be logged:
// const char *log_message = request_info->ev_data;
MG_EVENT_LOG,
// SSL initialization, sent before certificate setup.
// If callback returns non-NULL, Mongoose does not set up certificates.
// ev_data contains server's OpenSSL context:
// SSL_CTX *ssl_context = request_info->ev_data;
MG_INIT_SSL,
// Sent on HTTP connect, before websocket handshake.
// If user callback returns NULL, then mongoose proceeds
// with handshake, otherwise it closes the connection.
// ev_data contains NULL.
MG_WEBSOCKET_CONNECT,
// Handshake has been successfully completed.
// Callback's return value is ignored.
// ev_data contains NULL.
MG_WEBSOCKET_READY,
// Incoming message from the client, data could be read with mg_read().
// If user callback returns non-NULL, mongoose closes the websocket.
// ev_data contains NULL.
MG_WEBSOCKET_MESSAGE,
// Client has closed the connection.
// Callback's return value is ignored.
// ev_data contains NULL.
MG_WEBSOCKET_CLOSE,
}; };
...@@ -155,12 +194,7 @@ int mg_modify_passwords_file(const char *passwords_file_name, ...@@ -155,12 +194,7 @@ int mg_modify_passwords_file(const char *passwords_file_name,
// Return information associated with the request. // Return information associated with the request.
// These functions always succeed. struct mg_request_info *mg_get_request_info(struct mg_connection *);
const struct mg_request_info *mg_get_request_info(const struct mg_connection *);
void *mg_get_user_data(struct mg_connection *);
const char *mg_get_log_message(const struct mg_connection *);
int mg_get_reply_status_code(const struct mg_connection *);
void *mg_get_ssl_context(const struct mg_connection *);
// Send data to the client. // Send data to the client.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment