Commit 2ac970fa authored by Sergey Lyubka's avatar Sergey Lyubka

Moving to event-based API, temporary breaking tests

parent de35a353
......@@ -339,20 +339,17 @@ static const char *test_next_option(void) {
return NULL;
}
static int cb1(struct mg_connection *conn) {
int result = MG_REQUEST_NOT_PROCESSED;
if (!strcmp(conn->uri, "/cb1")) {
static int evh1(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_HTTP_ERROR) {
mg_printf(conn, "HTTP/1.0 404 NF\r\n\r\nERR: %d", conn->status_code);
return MG_TRUE;
} else if (ev == MG_REQ_BEGIN && !strcmp(conn->uri, "/cb1")) {
mg_printf(conn, "%s %s %s",
conn->server_param == NULL ? "?" : (char *) conn->server_param,
conn->connection_param == NULL ? "?" : "!", conn->remote_ip);
result = MG_REQUEST_PROCESSED;
return MG_TRUE;
}
return result;
}
static int error_handler(struct mg_connection *conn) {
mg_printf(conn, "HTTP/1.0 404 NF\r\n\r\nERR: %d", conn->status_code);
return MG_ERROR_PROCESSED;
return MG_FALSE;
}
static int ts1(struct mg_connection *conn) {
......@@ -379,16 +376,14 @@ static int ts2(struct mg_connection *conn) {
static const char *test_server(void) {
char buf1[100] = "", buf2[100] = "";
struct mg_server *server = mg_create_server((void *) "foo");
struct mg_server *server = mg_create_server((void *) "foo", evh1);
ASSERT(server != NULL);
ASSERT(mg_set_option(server, "listening_port", LISTENING_ADDR) == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
mg_set_request_handler(server, cb1);
mg_set_http_error_handler(server, error_handler);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, ts1, buf1) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, ts2, buf2) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, buf1) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, buf2) == 1);
{ int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
ASSERT(strcmp(buf1, "foo ? 127.0.0.1") == 0);
......@@ -464,15 +459,14 @@ static int cb3(struct mg_connection *conn) {
static const char *test_mg_connect(void) {
char buf2[40] = "", buf3[40] = "", buf4[40] = "";
struct mg_server *server = mg_create_server(NULL);
struct mg_server *server = mg_create_server(NULL, NULL); // cb4h
ASSERT(mg_set_option(server, "listening_port", LISTENING_ADDR) == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
mg_set_request_handler(server, cb4h);
ASSERT(mg_connect(server, "", 0, 0, NULL, NULL) == 0);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, cb2, buf2) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, buf2) == 1);
ASSERT(mg_connect(server, "127.0.0.1", 29, 0, cb3, buf3) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, cb4, buf4) == 1);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0, buf4) == 1);
{ int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
......@@ -562,7 +556,7 @@ static const char *test_ssl(void) {
#endif
static const char *test_mg_set_option(void) {
struct mg_server *server = mg_create_server(NULL);
struct mg_server *server = mg_create_server(NULL, NULL);
ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
ASSERT(mg_get_option(server, "listening_port")[0] != '\0');
mg_destroy_server(&server);
......@@ -571,7 +565,7 @@ static const char *test_mg_set_option(void) {
static const char *test_rewrites(void) {
char buf1[100] = "xx";
struct mg_server *server = mg_create_server(NULL);
struct mg_server *server = mg_create_server(NULL, NULL);
ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
......
This diff is collapsed.
......@@ -55,26 +55,31 @@ struct mg_connection {
int wsbits; // First byte of the websocket frame
void *server_param; // Parameter passed to mg_add_uri_handler()
void *connection_param; // Placeholder for connection-specific data
void *callback_param; // Used by mg_iterate_over_connections()
};
struct mg_server; // Opaque structure describing server instance
typedef int (*mg_handler_t)(struct mg_connection *);
enum mg_result { MG_FALSE, MG_TRUE };
enum mg_event {
MG_POLL, // Callback return value is ignored
MG_AUTH, // If callback returns MG_FALSE, authentication fails
MG_REQ_BEGIN, // If callback returns MG_FALSE, Mongoose continues with req
MG_REQ_END, // Callback return value is ignored
MG_CLOSE, // Connection is closed
MG_CONNECT, // If callback returns MG_FALSE, connect fails
MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
};
typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
// Server management functions
struct mg_server *mg_create_server(void *server_param);
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
void mg_destroy_server(struct mg_server **);
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
int mg_poll_server(struct mg_server *, int milliseconds);
void mg_set_request_handler(struct mg_server *, mg_handler_t);
void mg_set_http_close_handler(struct mg_server *, mg_handler_t);
void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
void mg_set_auth_handler(struct mg_server *, mg_handler_t);
const char **mg_get_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_set_listening_socket(struct mg_server *, int sock);
int mg_get_listening_socket(struct mg_server *);
void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
void mg_iterate_over_connections(struct mg_server *, mg_handler_t);
// Connection management functions
void mg_send_status(struct mg_connection *, int status_code);
......@@ -115,8 +120,7 @@ enum {
MG_CONNECT_SUCCESS, MG_CONNECT_FAILURE,
MG_DOWNLOAD_SUCCESS, MG_DOWNLOAD_FAILURE
};
int mg_connect(struct mg_server *, const char *host, int port, int use_ssl,
mg_handler_t handler, void *param);
struct mg_connection *mg_connect(struct mg_server *, const char *, int, int);
#ifdef __cplusplus
}
......
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