Skip to content

Commit

Permalink
Changing API: callback doesnt get mg_request_info pointer anymore, bu…
Browse files Browse the repository at this point in the history
…t it is possible to get it using mg_get_request_info()
  • Loading branch information
cpq committed Aug 3, 2012
1 parent e8f3132 commit 0e0091e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion bindings/python/example.py
Expand Up @@ -8,7 +8,8 @@
import sys

# Handle /show and /form URIs.
def EventHandler(event, conn, info):
def EventHandler(event, conn):
info = conn.info
if event == mongoose.HTTP_ERROR:
conn.printf('%s', 'HTTP/1.0 200 OK\r\n')
conn.printf('%s', 'Content-Type: text/plain\r\n\r\n')
Expand Down
11 changes: 5 additions & 6 deletions bindings/python/mongoose.py
Expand Up @@ -73,10 +73,7 @@ class mg_request_info(ctypes.Structure):
]


mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p,
ctypes.c_int,
ctypes.c_void_p,
ctypes.POINTER(mg_request_info))
mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)


class Connection(object):
Expand All @@ -86,6 +83,7 @@ class Connection(object):
def __init__(self, mongoose, connection):
self.m = mongoose
self.conn = ctypes.c_void_p(connection)
self.info = self.m.dll.mg_get_request_info(self.conn).contents

def get_header(self, name):
val = self.m.dll.mg_get_header(self.conn, name)
Expand Down Expand Up @@ -132,14 +130,15 @@ def __init__(self, callback, **kwargs):
self.dll.mg_get_var.restype = ctypes.c_int
self.dll.mg_get_cookie.restype = ctypes.c_int
self.dll.mg_get_option.restype = ctypes.c_char_p
self.dll.mg_get_request_info.restype = ctypes.POINTER(mg_request_info)

if callback:
# Create a closure that will be called by the shared library.
def func(event, connection, request_info):
def func(event, connection):
# Wrap connection pointer into the connection
# object and call Python callback
conn = Connection(self, connection)
return callback(event, conn, request_info.contents) and 1 or 0
return callback(event, conn) and 1 or 0

# Convert the closure into C callable object
self.callback = mg_callback_t(func)
Expand Down
9 changes: 6 additions & 3 deletions mongoose.c
Expand Up @@ -492,7 +492,7 @@ const char **mg_get_valid_option_names(void) {
static void *call_user(struct mg_connection *conn, enum mg_event event) {
conn->request_info.user_data = conn->ctx->user_data;
return conn->ctx->user_callback == NULL ? NULL :
conn->ctx->user_callback(event, conn, &conn->request_info);
conn->ctx->user_callback(event, conn);
}

static int get_option_index(const char *name) {
Expand Down Expand Up @@ -588,6 +588,10 @@ const char *mg_version(void) {
return MONGOOSE_VERSION;
}

const struct mg_request_info *mg_get_request_info(struct mg_connection *conn) {
return &conn->request_info;
}

static void mg_strlcpy(register char *dst, register const char *src, size_t n) {
for (; *src != '\0' && n > 1; n--) {
*dst++ = *src++;
Expand Down Expand Up @@ -3758,8 +3762,7 @@ static int set_ssl_option(struct mg_context *ctx) {
} else if (ctx->user_callback != NULL) {
memset(&request_info, 0, sizeof(request_info));
request_info.user_data = ctx->user_data;
ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx,
&request_info);
ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) ctx->ssl_ctx);
}

if (ctx->ssl_ctx != NULL && pem != NULL &&
Expand Down
10 changes: 7 additions & 3 deletions mongoose.h
Expand Up @@ -68,7 +68,6 @@ enum mg_event {
// event: which event has been triggered.
// conn: opaque connection handler. Could be used to read, write data to the
// client, etc. See functions below that have "mg_connection *" arg.
// request_info: Information about HTTP request.
//
// Return:
// If handler returns non-NULL, that means that handler has processed the
Expand All @@ -78,8 +77,7 @@ enum mg_event {
// the request. Handler must not send any data to the client in this case.
// Mongoose proceeds with request handling as if nothing happened.
typedef void * (*mg_callback_t)(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info);
struct mg_connection *conn);


// Start web server.
Expand Down Expand Up @@ -151,6 +149,12 @@ int mg_modify_passwords_file(const char *passwords_file_name,
const char *user,
const char *password);


// Return mg_request_info structure associated with the request.
// Always succeeds.
const struct mg_request_info *mg_get_request_info(struct mg_connection *);


// Send data to the client.
int mg_write(struct mg_connection *, const void *buf, size_t len);

Expand Down
4 changes: 2 additions & 2 deletions test/embed.c
Expand Up @@ -155,8 +155,8 @@ static const struct test_config {
};

static void *callback(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info) {
struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
int i;

for (i = 0; test_config[i].uri != NULL; i++) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit_test.c
Expand Up @@ -129,8 +129,8 @@ static void test_remove_double_dots() {

static const char *fetch_data = "hello world!\n";
static void *event_handler(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info) {
struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
mg_printf(conn, "HTTP/1.1 200 OK\r\n"
"Content-Length: %d\r\n"
Expand Down

0 comments on commit 0e0091e

Please sign in to comment.