23 #define sleep(x) Sleep((x) * 1000) 33 #define SSE_CONNECTION ((void *) 1) 35 static void elog(
int do_exit,
const char *fmt, ...) {
41 if (do_exit) exit(EXIT_FAILURE);
59 (
unsigned long) time(NULL), (
const char *) param);
68 return stat(path, &st) != 0 ? 0 : S_ISDIR(st.st_mode) == 0 ? 1 : 2;
84 strncat(path,
"/index.html",
sizeof(path) - strlen(path) - 1);
86 printf(
"PATH: [%s]\n", path);
90 if ((fp = fopen(path,
"rb")) != NULL) {
91 printf(
"Serving [%s] locally \n", path);
94 while ((n =
fread(buf, 1,
sizeof(buf), fp)) > 0) {
105 return is_local_file(uri, path,
sizeof(path)) || strcmp(uri,
"/api/sse") == 0;
109 static const char target_url[] =
"http://cesanta.com";
110 static int target_url_size =
sizeof(target_url) - 1;
117 host == NULL ?
"" : host);
123 if (memcmp(conn->
uri, target_url, target_url_size) == 0 &&
125 conn->
uri += target_url_size;
128 if (strcmp(conn->
uri,
"/api/sse") == 0) {
130 mg_printf(conn,
"%s",
"HTTP/1.0 200 OK\r\n" 131 "Content-Type: text/event-stream\r\n" 132 "Cache-Control: no-cache\r\n\r\n");
136 if (host != NULL && strstr(host,
"cesanta") != NULL) {
142 !strcmp(host,
"oracle.com")) {
157 if (err_msg != NULL) {
158 elog(1,
"Error setting [%s]: [%s]", opt, err_msg);
162 int main(
int argc,
char *argv[]) {
163 const char *
port =
"2014", *
dump = NULL, *
root =
"proxy_web_root";
167 for (i = 1; i < argc; i++) {
168 if (strcmp(argv[i],
"-port") == 0 && i + 1 < argc) {
170 }
else if (strcmp(argv[i],
"-root") == 0 && i + 1 < argc) {
172 }
else if (strcmp(argv[i],
"-dump") == 0 && i + 1 < argc) {
175 elog(1,
"Usage: %s [-cert FILE] [-ca_cert FILE] [-port PORT]", argv[0]);
184 setopt(s_server,
"enable_proxy",
"yes");
185 setopt(s_server,
"document_root", root);
186 setopt(s_server,
"listening_port", port);
static struct mg_server * s_server
void mg_send_header(struct mg_connection *c, const char *name, const char *v)
static int sse_push(struct mg_connection *conn, enum mg_event ev)
static int exists(const char *path)
int mg_terminate_ssl(struct mg_connection *c, const char *cert)
void * mg_start_thread(void *(*func)(void *), void *param)
int stat(const char *path, struct stat *buffer)
static void signal_handler(int sig_num)
const char * mg_get_header(const struct mg_connection *ri, const char *s)
static void * sse_pusher_thread_func(void *param)
static int is_resource_present_locally(const char *uri)
const char * mg_get_mime_type(const char *path, const char *default_mime_type)
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
const char * mg_get_option(const struct mg_server *server, const char *name)
time_t mg_poll_server(struct mg_server *server, int milliseconds)
static int s_received_signal
static void setopt(struct mg_server *s, const char *opt, const char *val)
static int proxy_event_handler(struct mg_connection *conn, enum mg_event ev)
static void elog(int do_exit, const char *fmt,...)
static int try_to_serve_locally(struct mg_connection *conn)
static int is_local_file(const char *uri, char *path, size_t path_len)
int main(int argc, char *argv[])
static unsigned short port
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
size_t mg_printf(struct mg_connection *conn, const char *fmt,...)
const char * request_method
static void dump(const char *text, FILE *stream, unsigned char *ptr, size_t size, char nohex)
void mg_destroy_server(struct mg_server **server)
void mg_wakeup_server_ex(struct mg_server *server, mg_handler_t cb, const char *fmt,...)
size_t mg_send_data(struct mg_connection *c, const void *data, int data_len)