1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-07-29 21:01:13 +03:00

replace Windows CRLF with Unix newline

Signed-off-by: Danny Al-Gaaf <danny.al-gaaf@bisect.de>
This commit is contained in:
Danny Al-Gaaf
2015-03-23 16:38:15 +01:00
parent a36f756be3
commit ec9d5413a8
6 changed files with 1336 additions and 1336 deletions

View File

@ -1,187 +1,187 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include "WebSockCallbacks.h" #include "WebSockCallbacks.h"
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#define mg_sleep(x) Sleep(x) #define mg_sleep(x) Sleep(x)
#else #else
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#define mg_sleep(x) usleep((x) * 1000) #define mg_sleep(x) usleep((x) * 1000)
#endif #endif
static void send_to_all_websockets(struct mg_context *ctx, const char * data, int data_len) { static void send_to_all_websockets(struct mg_context *ctx, const char * data, int data_len) {
int i; int i;
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
mg_lock_context(ctx); mg_lock_context(ctx);
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) { for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (ws_ctx->socketList[i] && (ws_ctx->socketList[i]->webSockState==2)) { if (ws_ctx->socketList[i] && (ws_ctx->socketList[i]->webSockState==2)) {
mg_websocket_write(ws_ctx->socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len); mg_websocket_write(ws_ctx->socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
} }
} }
mg_unlock_context(ctx); mg_unlock_context(ctx);
} }
void websocket_ready_handler(struct mg_connection *conn) { void websocket_ready_handler(struct mg_connection *conn) {
int i; int i;
struct mg_request_info * rq = mg_get_request_info(conn); struct mg_request_info * rq = mg_get_request_info(conn);
struct mg_context * ctx = mg_get_context(conn); struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo)); tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
assert(wsock); assert(wsock);
wsock->webSockState = 0; wsock->webSockState = 0;
rq->conn_data = wsock; rq->conn_data = wsock;
mg_lock_context(ctx); mg_lock_context(ctx);
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) { for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (0==ws_ctx->socketList[i]) { if (0==ws_ctx->socketList[i]) {
ws_ctx->socketList[i] = wsock; ws_ctx->socketList[i] = wsock;
wsock->conn = conn; wsock->conn = conn;
wsock->webSockState = 1; wsock->webSockState = 1;
break; break;
} }
} }
printf("\nNew websocket attached: %s:%u\n", rq->remote_addr, rq->remote_port); printf("\nNew websocket attached: %s:%u\n", rq->remote_addr, rq->remote_port);
mg_unlock_context(ctx); mg_unlock_context(ctx);
} }
static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) { static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) {
int i; int i;
if (wsock) { if (wsock) {
wsock->webSockState = 99; wsock->webSockState = 99;
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) { for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (wsock==ws_ctx->socketList[i]) { if (wsock==ws_ctx->socketList[i]) {
ws_ctx->socketList[i] = 0; ws_ctx->socketList[i] = 0;
break; break;
} }
} }
printf("\nClose websocket attached: %s:%u\n", mg_get_request_info(wsock->conn)->remote_addr, mg_get_request_info(wsock->conn)->remote_port); printf("\nClose websocket attached: %s:%u\n", mg_get_request_info(wsock->conn)->remote_addr, mg_get_request_info(wsock->conn)->remote_port);
free(wsock); free(wsock);
} }
} }
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) { int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) {
struct mg_request_info * rq = mg_get_request_info(conn); struct mg_request_info * rq = mg_get_request_info(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data; tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
struct mg_context * ctx = mg_get_context(conn); struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
char msg[128]; char msg[128];
mg_lock_context(ctx); mg_lock_context(ctx);
if (flags==136) { if (flags==136) {
// close websock // close websock
websocket_done(ws_ctx, wsock); websocket_done(ws_ctx, wsock);
rq->conn_data = 0; rq->conn_data = 0;
mg_unlock_context(ctx); mg_unlock_context(ctx);
return 1; return 1;
} }
if (((data_len>=5) && (data_len<100) && (flags==129)) || (flags==130)) { if (((data_len>=5) && (data_len<100) && (flags==129)) || (flags==130)) {
// init command // init command
if ((wsock->webSockState==1) && (!memcmp(data,"init ",5))) { if ((wsock->webSockState==1) && (!memcmp(data,"init ",5))) {
char * chk; char * chk;
unsigned long gid; unsigned long gid;
memcpy(msg,data+5,data_len-5); memcpy(msg,data+5,data_len-5);
msg[data_len-5]=0; msg[data_len-5]=0;
gid = strtoul(msg,&chk,10); gid = strtoul(msg,&chk,10);
wsock->initId = gid; wsock->initId = gid;
if (gid>0 && chk!=NULL && *chk==0) { if (gid>0 && chk!=NULL && *chk==0) {
wsock->webSockState = 2; wsock->webSockState = 2;
} }
mg_unlock_context(ctx); mg_unlock_context(ctx);
return 1; return 1;
} }
// chat message // chat message
if ((wsock->webSockState==2) && (!memcmp(data,"msg ",4))) { if ((wsock->webSockState==2) && (!memcmp(data,"msg ",4))) {
send_to_all_websockets(ctx, data, data_len); send_to_all_websockets(ctx, data, data_len);
mg_unlock_context(ctx); mg_unlock_context(ctx);
return 1; return 1;
} }
} }
// keep alive // keep alive
if ((data_len==4) && !memcmp(data,"ping",4)) { if ((data_len==4) && !memcmp(data,"ping",4)) {
mg_unlock_context(ctx); mg_unlock_context(ctx);
return 1; return 1;
} }
mg_unlock_context(ctx); mg_unlock_context(ctx);
return 0; return 0;
} }
void connection_close_handler(struct mg_connection *conn) { void connection_close_handler(struct mg_connection *conn) {
struct mg_request_info * rq = mg_get_request_info(conn); struct mg_request_info * rq = mg_get_request_info(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data; tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
struct mg_context * ctx = mg_get_context(conn); struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
mg_lock_context(ctx); mg_lock_context(ctx);
websocket_done(ws_ctx, wsock); websocket_done(ws_ctx, wsock);
rq->conn_data = 0; rq->conn_data = 0;
mg_unlock_context(ctx); mg_unlock_context(ctx);
} }
static void * eventMain(void * arg) { static void * eventMain(void * arg) {
char msg[256]; char msg[256];
struct mg_context *ctx = (struct mg_context *)arg; struct mg_context *ctx = (struct mg_context *)arg;
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
ws_ctx->runLoop = 1; ws_ctx->runLoop = 1;
while (ws_ctx->runLoop) { while (ws_ctx->runLoop) {
time_t t = time(0); time_t t = time(0);
struct tm * timestr = localtime(&t); struct tm * timestr = localtime(&t);
sprintf(msg,"title %s",asctime(timestr)); sprintf(msg,"title %s",asctime(timestr));
send_to_all_websockets(ctx, msg, strlen(msg)); send_to_all_websockets(ctx, msg, strlen(msg));
mg_sleep(1000); mg_sleep(1000);
} }
return NULL; return NULL;
} }
void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len) { void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len) {
char buffer[260]; char buffer[260];
if (data_len<=256) { if (data_len<=256) {
strcpy(buffer, "msg "); strcpy(buffer, "msg ");
memcpy(buffer+4, data, data_len); memcpy(buffer+4, data, data_len);
send_to_all_websockets(ctx, buffer, data_len+4); send_to_all_websockets(ctx, buffer, data_len+4);
} }
} }
void websock_init_lib(struct mg_context *ctx) { void websock_init_lib(struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
memset(ws_ctx,0,sizeof(*ws_ctx)); memset(ws_ctx,0,sizeof(*ws_ctx));
/* todo: use mg_start_thread_id instead of mg_start_thread */ /* todo: use mg_start_thread_id instead of mg_start_thread */
mg_start_thread(eventMain, ctx); mg_start_thread(eventMain, ctx);
} }
void websock_exit_lib(struct mg_context *ctx) { void websock_exit_lib(struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
ws_ctx->runLoop = 0; ws_ctx->runLoop = 0;
/* todo: wait for the thread instead of a timeout */ /* todo: wait for the thread instead of a timeout */
mg_sleep(2000); mg_sleep(2000);
} }

View File

@ -1,39 +1,39 @@
#ifndef WEBSOCKCALLBACKS_H_INCLUDED #ifndef WEBSOCKCALLBACKS_H_INCLUDED
#define WEBSOCKCALLBACKS_H_INCLUDED #define WEBSOCKCALLBACKS_H_INCLUDED
#include "civetweb.h" #include "civetweb.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef struct tWebSockInfo { typedef struct tWebSockInfo {
int webSockState; int webSockState;
unsigned long initId; unsigned long initId;
struct mg_connection *conn; struct mg_connection *conn;
} tWebSockInfo; } tWebSockInfo;
#define MAX_NUM_OF_WEBSOCKS (256) #define MAX_NUM_OF_WEBSOCKS (256)
typedef struct tWebSockContext { typedef struct tWebSockContext {
int runLoop; int runLoop;
void * thread_id; void * thread_id;
tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS]; tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS];
} tWebSockContext; } tWebSockContext;
void websock_init_lib(struct mg_context *ctx); void websock_init_lib(struct mg_context *ctx);
void websock_exit_lib(struct mg_context *ctx); void websock_exit_lib(struct mg_context *ctx);
void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len); void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len);
void websocket_ready_handler(struct mg_connection *conn); void websocket_ready_handler(struct mg_connection *conn);
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len); int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len);
void connection_close_handler(struct mg_connection *conn); void connection_close_handler(struct mg_connection *conn);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -1510,25 +1510,25 @@ static int MakeConsole() {
if (!ok) { if (!ok) {
if (!AttachConsole(ATTACH_PARENT_PROCESS)) { if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
FreeConsole(); FreeConsole();
if (!AllocConsole()) { if (!AllocConsole()) {
err = GetLastError(); err = GetLastError();
if (err==ERROR_ACCESS_DENIED) { if (err==ERROR_ACCESS_DENIED) {
MessageBox(NULL, "Insufficient rights to create a console window", "Error", MB_ICONERROR); MessageBox(NULL, "Insufficient rights to create a console window", "Error", MB_ICONERROR);
} }
} }
AttachConsole(GetCurrentProcessId()); AttachConsole(GetCurrentProcessId());
} }
ok = (GetConsoleWindow() != NULL); ok = (GetConsoleWindow() != NULL);
if (ok) { if (ok) {
freopen("CONIN$", "r", stdin); freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr); freopen("CONOUT$", "w", stderr);
} }
} }
if (ok) { if (ok) {
SetConsoleTitle(server_name); SetConsoleTitle(server_name);
} }
return ok; return ok;

1790
src/third_party/lfs.c vendored

File diff suppressed because it is too large Load Diff

34
src/third_party/lfs.h vendored
View File

@ -1,17 +1,17 @@
/* /*
** LuaFileSystem ** LuaFileSystem
** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem) ** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
** **
** $Id: lfs.h,v 1.5 2008/02/19 20:08:23 mascarenhas Exp $ ** $Id: lfs.h,v 1.5 2008/02/19 20:08:23 mascarenhas Exp $
*/ */
/* Define 'chdir' for systems that do not implement it */ /* Define 'chdir' for systems that do not implement it */
#ifdef NO_CHDIR #ifdef NO_CHDIR
#define chdir(p) (-1) #define chdir(p) (-1)
#define chdir_error "Function 'chdir' not provided by system" #define chdir_error "Function 'chdir' not provided by system"
#else #else
#define chdir_error strerror(errno) #define chdir_error strerror(errno)
#endif #endif
int luaopen_lfs (lua_State *L); int luaopen_lfs (lua_State *L);

View File

@ -1,182 +1,182 @@
// Copyright (c) 2004-2009 Sergey Lyubka // Copyright (c) 2004-2009 Sergey Lyubka
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights // in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
// //
// Unit test for the civetweb web server. Tests embedded API. // Unit test for the civetweb web server. Tests embedded API.
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifndef _WIN32 #ifndef _WIN32
#include <unistd.h> #include <unistd.h>
#endif #endif
#include "civetweb.h" #include "civetweb.h"
#if !defined(LISTENING_PORT) #if !defined(LISTENING_PORT)
#define LISTENING_PORT "23456" #define LISTENING_PORT "23456"
#endif #endif
static const char *standard_reply = "HTTP/1.1 200 OK\r\n" static const char *standard_reply = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
"Connection: close\r\n\r\n"; "Connection: close\r\n\r\n";
static void test_get_var(struct mg_connection *conn, static void test_get_var(struct mg_connection *conn,
const struct mg_request_info *ri) { const struct mg_request_info *ri) {
char *var, *buf; char *var, *buf;
size_t buf_len; size_t buf_len;
const char *cl; const char *cl;
int var_len; int var_len;
mg_printf(conn, "%s", standard_reply); mg_printf(conn, "%s", standard_reply);
buf_len = 0; buf_len = 0;
var = buf = NULL; var = buf = NULL;
cl = mg_get_header(conn, "Content-Length"); cl = mg_get_header(conn, "Content-Length");
mg_printf(conn, "cl: %p\n", cl); mg_printf(conn, "cl: %p\n", cl);
if ((!strcmp(ri->request_method, "POST") || if ((!strcmp(ri->request_method, "POST") ||
!strcmp(ri->request_method, "PUT")) !strcmp(ri->request_method, "PUT"))
&& cl != NULL) { && cl != NULL) {
buf_len = atoi(cl); buf_len = atoi(cl);
buf = malloc(buf_len); buf = malloc(buf_len);
/* Read in two pieces, to test continuation */ /* Read in two pieces, to test continuation */
if (buf_len > 2) { if (buf_len > 2) {
mg_read(conn, buf, 2); mg_read(conn, buf, 2);
mg_read(conn, buf + 2, buf_len - 2); mg_read(conn, buf + 2, buf_len - 2);
} else { } else {
mg_read(conn, buf, buf_len); mg_read(conn, buf, buf_len);
} }
} else if (ri->query_string != NULL) { } else if (ri->query_string != NULL) {
buf_len = strlen(ri->query_string); buf_len = strlen(ri->query_string);
buf = malloc(buf_len + 1); buf = malloc(buf_len + 1);
strcpy(buf, ri->query_string); strcpy(buf, ri->query_string);
} }
var = malloc(buf_len + 1); var = malloc(buf_len + 1);
var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1); var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1);
mg_printf(conn, "Value: [%s]\n", var); mg_printf(conn, "Value: [%s]\n", var);
mg_printf(conn, "Value size: [%d]\n", var_len); mg_printf(conn, "Value size: [%d]\n", var_len);
free(buf); free(buf);
free(var); free(var);
} }
static void test_get_header(struct mg_connection *conn, static void test_get_header(struct mg_connection *conn,
const struct mg_request_info *ri) { const struct mg_request_info *ri) {
const char *value; const char *value;
int i; int i;
mg_printf(conn, "%s", standard_reply); mg_printf(conn, "%s", standard_reply);
printf("HTTP headers: %d\n", ri->num_headers); printf("HTTP headers: %d\n", ri->num_headers);
for (i = 0; i < ri->num_headers; i++) { for (i = 0; i < ri->num_headers; i++) {
printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value); printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value);
} }
value = mg_get_header(conn, "Host"); value = mg_get_header(conn, "Host");
if (value != NULL) { if (value != NULL) {
mg_printf(conn, "Value: [%s]", value); mg_printf(conn, "Value: [%s]", value);
} }
} }
static void test_get_request_info(struct mg_connection *conn, static void test_get_request_info(struct mg_connection *conn,
const struct mg_request_info *ri) { const struct mg_request_info *ri) {
int i; int i;
mg_printf(conn, "%s", standard_reply); mg_printf(conn, "%s", standard_reply);
mg_printf(conn, "Method: [%s]\n", ri->request_method); mg_printf(conn, "Method: [%s]\n", ri->request_method);
mg_printf(conn, "URI: [%s]\n", ri->uri); mg_printf(conn, "URI: [%s]\n", ri->uri);
mg_printf(conn, "HTTP version: [%s]\n", ri->http_version); mg_printf(conn, "HTTP version: [%s]\n", ri->http_version);
for (i = 0; i < ri->num_headers; i++) { for (i = 0; i < ri->num_headers; i++) {
mg_printf(conn, "HTTP header [%s]: [%s]\n", mg_printf(conn, "HTTP header [%s]: [%s]\n",
ri->http_headers[i].name, ri->http_headers[i].name,
ri->http_headers[i].value); ri->http_headers[i].value);
} }
mg_printf(conn, "Query string: [%s]\n", mg_printf(conn, "Query string: [%s]\n",
ri->query_string ? ri->query_string: ""); ri->query_string ? ri->query_string: "");
mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip); mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip);
mg_printf(conn, "Remote port: [%d]\n", ri->remote_port); mg_printf(conn, "Remote port: [%d]\n", ri->remote_port);
mg_printf(conn, "Remote user: [%s]\n", mg_printf(conn, "Remote user: [%s]\n",
ri->remote_user ? ri->remote_user : ""); ri->remote_user ? ri->remote_user : "");
} }
static void test_error(struct mg_connection *conn, static void test_error(struct mg_connection *conn,
const struct mg_request_info *ri) { const struct mg_request_info *ri) {
int status = (int) ri->ev_data; int status = (int) ri->ev_data;
mg_printf(conn, "HTTP/1.1 %d XX\r\n" mg_printf(conn, "HTTP/1.1 %d XX\r\n"
"Conntection: close\r\n\r\n", status); "Conntection: close\r\n\r\n", status);
mg_printf(conn, "Error: [%d]", status); mg_printf(conn, "Error: [%d]", status);
} }
static void test_post(struct mg_connection *conn, static void test_post(struct mg_connection *conn,
const struct mg_request_info *ri) { const struct mg_request_info *ri) {
const char *cl; const char *cl;
char *buf; char *buf;
int len; int len;
mg_printf(conn, "%s", standard_reply); mg_printf(conn, "%s", standard_reply);
if (strcmp(ri->request_method, "POST") == 0 && if (strcmp(ri->request_method, "POST") == 0 &&
(cl = mg_get_header(conn, "Content-Length")) != NULL) { (cl = mg_get_header(conn, "Content-Length")) != NULL) {
len = atoi(cl); len = atoi(cl);
if ((buf = malloc(len)) != NULL) { if ((buf = malloc(len)) != NULL) {
mg_write(conn, buf, len); mg_write(conn, buf, len);
free(buf); free(buf);
} }
} }
} }
static const struct test_config { static const struct test_config {
enum mg_event event; enum mg_event event;
const char *uri; const char *uri;
void (*func)(struct mg_connection *, const struct mg_request_info *); void (*func)(struct mg_connection *, const struct mg_request_info *);
} test_config[] = { } test_config[] = {
{MG_NEW_REQUEST, "/test_get_header", &test_get_header}, {MG_NEW_REQUEST, "/test_get_header", &test_get_header},
{MG_NEW_REQUEST, "/test_get_var", &test_get_var}, {MG_NEW_REQUEST, "/test_get_var", &test_get_var},
{MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info}, {MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info},
{MG_NEW_REQUEST, "/test_post", &test_post}, {MG_NEW_REQUEST, "/test_post", &test_post},
{MG_HTTP_ERROR, "", &test_error}, {MG_HTTP_ERROR, "", &test_error},
{0, NULL, NULL} {0, NULL, NULL}
}; };
static void *callback(enum mg_event event, static void *callback(enum mg_event event,
struct mg_connection *conn) { struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
int i; int i;
for (i = 0; test_config[i].uri != NULL; i++) { for (i = 0; test_config[i].uri != NULL; i++) {
if (event == test_config[i].event && if (event == test_config[i].event &&
(event == MG_HTTP_ERROR || (event == MG_HTTP_ERROR ||
!strcmp(request_info->uri, test_config[i].uri))) { !strcmp(request_info->uri, test_config[i].uri))) {
test_config[i].func(conn, request_info); test_config[i].func(conn, request_info);
return "processed"; return "processed";
} }
} }
return NULL; return NULL;
} }
int main(void) { int main(void) {
struct mg_context *ctx; struct mg_context *ctx;
const char *options[] = {"listening_ports", LISTENING_PORT, NULL}; const char *options[] = {"listening_ports", LISTENING_PORT, NULL};
ctx = mg_start(callback, NULL, options); ctx = mg_start(callback, NULL, options);
pause(); pause();
return 0; return 0;
} }