From 63e9cbed40527069cbe99d0d3fa34a63308d0485 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer <torsten@t3r.de> Date: Mon, 3 Mar 2014 21:59:16 +0100 Subject: [PATCH] Initial commit for mongoose httpd --- 3rdparty/CMakeLists.txt | 3 + 3rdparty/cjson/CMakeLists.txt | 11 + 3rdparty/cjson/LICENSE | 20 + 3rdparty/cjson/README | 247 ++ 3rdparty/cjson/cJSON.c | 596 +++ 3rdparty/cjson/cJSON.h | 143 + 3rdparty/cjson/test.c | 156 + 3rdparty/cjson/tests/test1 | 22 + 3rdparty/cjson/tests/test2 | 11 + 3rdparty/cjson/tests/test3 | 26 + 3rdparty/cjson/tests/test4 | 88 + 3rdparty/cjson/tests/test5 | 27 + 3rdparty/mongoose/CMakeLists.txt | 11 + 3rdparty/mongoose/LICENSE | 16 + 3rdparty/mongoose/mongoose.c | 4211 +++++++++++++++++++++ 3rdparty/mongoose/mongoose.h | 123 + src/Main/fg_init.cxx | 7 + src/Main/fg_io.cxx | 6 +- src/Main/options.cxx | 13 +- src/Network/CMakeLists.txt | 8 +- src/Network/http/CMakeLists.txt | 21 + src/Network/http/HTTPRequest.hxx | 65 + src/Network/http/HTTPResponse.hxx | 51 + src/Network/http/JsonUriHandler.cxx | 109 + src/Network/http/JsonUriHandler.hxx | 38 + src/Network/http/PropertyUriHandler.cxx | 411 ++ src/Network/http/PropertyUriHandler.hxx | 38 + src/Network/http/RunUriHandler.cxx | 55 + src/Network/http/RunUriHandler.hxx | 38 + src/Network/http/ScreenshotUriHandler.cxx | 39 + src/Network/http/ScreenshotUriHandler.hxx | 38 + src/Network/http/httpd.cxx | 244 ++ src/Network/{ => http}/httpd.hxx | 46 +- src/Network/http/urihandler.hxx | 61 + src/Network/httpd.cxx | 445 --- 35 files changed, 6963 insertions(+), 481 deletions(-) create mode 100644 3rdparty/cjson/CMakeLists.txt create mode 100644 3rdparty/cjson/LICENSE create mode 100644 3rdparty/cjson/README create mode 100644 3rdparty/cjson/cJSON.c create mode 100644 3rdparty/cjson/cJSON.h create mode 100644 3rdparty/cjson/test.c create mode 100644 3rdparty/cjson/tests/test1 create mode 100644 3rdparty/cjson/tests/test2 create mode 100644 3rdparty/cjson/tests/test3 create mode 100644 3rdparty/cjson/tests/test4 create mode 100644 3rdparty/cjson/tests/test5 create mode 100644 3rdparty/mongoose/CMakeLists.txt create mode 100644 3rdparty/mongoose/LICENSE create mode 100644 3rdparty/mongoose/mongoose.c create mode 100644 3rdparty/mongoose/mongoose.h create mode 100644 src/Network/http/CMakeLists.txt create mode 100644 src/Network/http/HTTPRequest.hxx create mode 100644 src/Network/http/HTTPResponse.hxx create mode 100644 src/Network/http/JsonUriHandler.cxx create mode 100644 src/Network/http/JsonUriHandler.hxx create mode 100644 src/Network/http/PropertyUriHandler.cxx create mode 100644 src/Network/http/PropertyUriHandler.hxx create mode 100644 src/Network/http/RunUriHandler.cxx create mode 100644 src/Network/http/RunUriHandler.hxx create mode 100644 src/Network/http/ScreenshotUriHandler.cxx create mode 100644 src/Network/http/ScreenshotUriHandler.hxx create mode 100644 src/Network/http/httpd.cxx rename src/Network/{ => http}/httpd.hxx (57%) create mode 100644 src/Network/http/urihandler.hxx delete mode 100644 src/Network/httpd.cxx diff --git a/3rdparty/CMakeLists.txt b/3rdparty/CMakeLists.txt index 72588c9fe..8bc8577f2 100644 --- a/3rdparty/CMakeLists.txt +++ b/3rdparty/CMakeLists.txt @@ -6,3 +6,6 @@ endif() if (ENABLE_IAX) add_subdirectory(iaxclient/lib) endif() + +add_subdirectory(mongoose) +add_subdirectory(cjson) diff --git a/3rdparty/cjson/CMakeLists.txt b/3rdparty/cjson/CMakeLists.txt new file mode 100644 index 000000000..e666d34eb --- /dev/null +++ b/3rdparty/cjson/CMakeLists.txt @@ -0,0 +1,11 @@ +include(FlightGearComponent) + +set(SOURCES + cJSON.c + ) + +set(HEADERS + cJSON.h + ) + +flightgear_component(CJSON "${SOURCES}" "${HEADERS}") diff --git a/3rdparty/cjson/LICENSE b/3rdparty/cjson/LICENSE new file mode 100644 index 000000000..fa0a438e2 --- /dev/null +++ b/3rdparty/cjson/LICENSE @@ -0,0 +1,20 @@ + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + 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 + THE SOFTWARE. + diff --git a/3rdparty/cjson/README b/3rdparty/cjson/README new file mode 100644 index 000000000..7531c049a --- /dev/null +++ b/3rdparty/cjson/README @@ -0,0 +1,247 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + 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 + THE SOFTWARE. +*/ + +Welcome to cJSON. + +cJSON aims to be the dumbest possible parser that you can get your job done with. +It's a single file of C, and a single header file. + +JSON is described best here: http://www.json.org/ +It's like XML, but fat-free. You use it to move data around, store things, or just +generally represent your program's state. + + +First up, how do I build? +Add cJSON.c to your project, and put cJSON.h somewhere in the header search path. +For example, to build the test app: + +gcc cJSON.c test.c -o test -lm +./test + + +As a library, cJSON exists to take away as much legwork as it can, but not get in your way. +As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it +in one of two modes: Auto and Manual. Let's have a quick run-through. + + +I lifted some JSON from this page: http://www.json.org/fatfree.html +That page inspired me to write cJSON, which is a parser that tries to share the same +philosophy as JSON itself. Simple, dumb, out of the way. + +Some JSON: +{ + "name": "Jack (\"Bee\") Nimble", + "format": { + "type": "rect", + "width": 1920, + "height": 1080, + "interlace": false, + "frame rate": 24 + } +} + +Assume that you got this from a file, a webserver, or magic JSON elves, whatever, +you have a char * to it. Everything is a cJSON struct. +Get it parsed: + cJSON *root = cJSON_Parse(my_json_string); + +This is an object. We're in C. We don't have objects. But we do have structs. +What's the framerate? + + cJSON *format = cJSON_GetObjectItem(root,"format"); + int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; + + +Want to change the framerate? + cJSON_GetObjectItem(format,"frame rate")->valueint=25; + +Back to disk? + char *rendered=cJSON_Print(root); + +Finished? Delete the root (this takes care of everything else). + cJSON_Delete(root); + +That's AUTO mode. If you're going to use Auto mode, you really ought to check pointers +before you dereference them. If you want to see how you'd build this struct in code? + cJSON *root,*fmt; + root=cJSON_CreateObject(); + cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); + cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject()); + cJSON_AddStringToObject(fmt,"type", "rect"); + cJSON_AddNumberToObject(fmt,"width", 1920); + cJSON_AddNumberToObject(fmt,"height", 1080); + cJSON_AddFalseToObject (fmt,"interlace"); + cJSON_AddNumberToObject(fmt,"frame rate", 24); + +Hopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup. +Look at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and +a few from elsewhere. + +What about manual mode? First up you need some detail. +Let's cover how the cJSON objects represent the JSON data. +cJSON doesn't distinguish arrays from objects in handling; just type. +Each cJSON has, potentially, a child, siblings, value, a name. + +The root object has: Object Type and a Child +The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling: +Sibling has type Object, name "format", and a child. +That child has type String, name "type", value "rect", and a sibling: +Sibling has type Number, name "width", value 1920, and a sibling: +Sibling has type Number, name "height", value 1080, and a sibling: +Sibling hs type False, name "interlace", and a sibling: +Sibling has type Number, name "frame rate", value 24 + +Here's the structure: +typedef struct cJSON { + struct cJSON *next,*prev; + struct cJSON *child; + + int type; + + char *valuestring; + int valueint; + double valuedouble; + + char *string; +} cJSON; + +By default all values are 0 unless set by virtue of being meaningful. + +next/prev is a doubly linked list of siblings. next takes you to your sibling, +prev takes you back from your sibling to you. +Only objects and arrays have a "child", and it's the head of the doubly linked list. +A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0. +The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in +cJSON.h + +A Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read +valuedouble. + +Any entry which is in the linked list which is the child of an object will have a "string" +which is the "name" of the entry. When I said "name" in the above example, that's "string". +"string" is the JSON name for the 'variable name' if you will. + +Now you can trivially walk the lists, recursively, and parse as you please. +You can invoke cJSON_Parse to get cJSON to parse for you, and then you can take +the root object, and traverse the structure (which is, formally, an N-tree), +and tokenise as you please. If you wanted to build a callback style parser, this is how +you'd do it (just an example, since these things are very specific): + +void parse_and_callback(cJSON *item,const char *prefix) +{ + while (item) + { + char *newprefix=malloc(strlen(prefix)+strlen(item->name)+2); + sprintf(newprefix,"%s/%s",prefix,item->name); + int dorecurse=callback(newprefix, item->type, item); + if (item->child && dorecurse) parse_and_callback(item->child,newprefix); + item=item->next; + free(newprefix); + } +} + +The prefix process will build you a separated list, to simplify your callback handling. +The 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or +let you invoke it per-item. For the item above, your callback might look like this: + +int callback(const char *name,int type,cJSON *item) +{ + if (!strcmp(name,"name")) { /* populate name */ } + else if (!strcmp(name,"format/type") { /* handle "rect" */ } + else if (!strcmp(name,"format/width") { /* 800 */ } + else if (!strcmp(name,"format/height") { /* 600 */ } + else if (!strcmp(name,"format/interlace") { /* false */ } + else if (!strcmp(name,"format/frame rate") { /* 24 */ } + return 1; +} + +Alternatively, you might like to parse iteratively. +You'd use: + +void parse_object(cJSON *item) +{ + int i; for (i=0;i<cJSON_GetArraySize(item);i++) + { + cJSON *subitem=cJSON_GetArrayItem(item,i); + // handle subitem. + } +} + +Or, for PROPER manual mode: + +void parse_object(cJSON *item) +{ + cJSON *subitem=item->child; + while (subitem) + { + // handle subitem + if (subitem->child) parse_object(subitem->child); + + subitem=subitem->next; + } +} + +Of course, this should look familiar, since this is just a stripped-down version +of the callback-parser. + +This should cover most uses you'll find for parsing. The rest should be possible +to infer.. and if in doubt, read the source! There's not a lot of it! ;) + + +In terms of constructing JSON data, the example code above is the right way to do it. +You can, of course, hand your sub-objects to other functions to populate. +Also, if you find a use for it, you can manually build the objects. +For instance, suppose you wanted to build an array of objects? + +cJSON *objects[24]; + +cJSON *Create_array_of_anything(cJSON **items,int num) +{ + int i;cJSON *prev, *root=cJSON_CreateArray(); + for (i=0;i<24;i++) + { + if (!i) root->child=objects[i]; + else prev->next=objects[i], objects[i]->prev=prev; + prev=objects[i]; + } + return root; +} + +and simply: Create_array_of_anything(objects,24); + +cJSON doesn't make any assumptions about what order you create things in. +You can attach the objects, as above, and later add children to each +of those objects. + +As soon as you call cJSON_Print, it renders the structure to text. + + + +The test.c code shows how to handle a bunch of typical cases. If you uncomment +the code, it'll load, parse and print a bunch of test files, also from json.org, +which are more complex than I'd care to try and stash into a const char array[]. + + +Enjoy cJSON! + + +- Dave Gamble, Aug 2009 diff --git a/3rdparty/cjson/cJSON.c b/3rdparty/cjson/cJSON.c new file mode 100644 index 000000000..31c43dd2e --- /dev/null +++ b/3rdparty/cjson/cJSON.c @@ -0,0 +1,596 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + 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 + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +#include <string.h> +#include <stdio.h> +#include <math.h> +#include <stdlib.h> +#include <float.h> +#include <limits.h> +#include <ctype.h> +#include "cJSON.h" + +static const char *ep; + +const char *cJSON_GetErrorPtr(void) {return ep;} + +static int cJSON_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); +} + +static void *(*cJSON_malloc)(size_t sz) = malloc; +static void (*cJSON_free)(void *ptr) = free; + +static char* cJSON_strdup(const char* str) +{ + size_t len; + char* copy; + + len = strlen(str) + 1; + if (!(copy = (char*)cJSON_malloc(len))) return 0; + memcpy(copy,str,len); + return copy; +} + +void cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (!hooks) { /* Reset hooks */ + cJSON_malloc = malloc; + cJSON_free = free; + return; + } + + cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc; + cJSON_free = (hooks->free_fn)?hooks->free_fn:free; +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(void) +{ + cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON)); + if (node) memset(node,0,sizeof(cJSON)); + return node; +} + +/* Delete a cJSON structure. */ +void cJSON_Delete(cJSON *c) +{ + cJSON *next; + while (c) + { + next=c->next; + if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child); + if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); + if (c->string) cJSON_free(c->string); + cJSON_free(c); + c=next; + } +} + +/* Parse the input text to generate a number, and populate the result into item. */ +static const char *parse_number(cJSON *item,const char *num) +{ + double n=0,sign=1,scale=0;int subscale=0,signsubscale=1; + + if (*num=='-') sign=-1,num++; /* Has sign? */ + if (*num=='0') num++; /* is zero */ + if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */ + if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */ + if (*num=='e' || *num=='E') /* Exponent? */ + { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ + while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */ + } + + n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ + + item->valuedouble=n; + item->valueint=(int)n; + item->type=cJSON_Number; + return num; +} + +/* Render the number nicely from the given item into a string. */ +static char *print_number(cJSON *item) +{ + char *str; + double d=item->valuedouble; + if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN) + { + str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ + if (str) sprintf(str,"%d",item->valueint); + } + else + { + str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */ + if (str) + { + if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d); + else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); + else sprintf(str,"%f",d); + } + } + return str; +} + +static unsigned parse_hex4(const char *str) +{ + unsigned h=0; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + return h; +} + +/* Parse the input text into an unescaped cstring, and populate item. */ +static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; +static const char *parse_string(cJSON *item,const char *str) +{ + const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2; + if (*str!='\"') {ep=str;return 0;} /* not a string! */ + + while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ + + out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */ + if (!out) return 0; + + ptr=str+1;ptr2=out; + while (*ptr!='\"' && *ptr) + { + if (*ptr!='\\') *ptr2++=*ptr++; + else + { + ptr++; + switch (*ptr) + { + case 'b': *ptr2++='\b'; break; + case 'f': *ptr2++='\f'; break; + case 'n': *ptr2++='\n'; break; + case 'r': *ptr2++='\r'; break; + case 't': *ptr2++='\t'; break; + case 'u': /* transcode utf16 to utf8. */ + uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */ + + if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */ + + if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */ + { + if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */ + uc2=parse_hex4(ptr+3);ptr+=6; + if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */ + uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF)); + } + + len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len; + + switch (len) { + case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 1: *--ptr2 =(uc | firstByteMark[len]); + } + ptr2+=len; + break; + default: *ptr2++=*ptr; break; + } + ptr++; + } + } + *ptr2=0; + if (*ptr=='\"') ptr++; + item->valuestring=out; + item->type=cJSON_String; + return ptr; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static char *print_string_ptr(const char *str) +{ + const char *ptr;char *ptr2,*out;int len=0;unsigned char token; + + if (!str) return cJSON_strdup(""); + ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} + + out=(char*)cJSON_malloc(len+3); + if (!out) return 0; + + ptr2=out;ptr=str; + *ptr2++='\"'; + while (*ptr) + { + if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; + else + { + *ptr2++='\\'; + switch (token=*ptr++) + { + case '\\': *ptr2++='\\'; break; + case '\"': *ptr2++='\"'; break; + case '\b': *ptr2++='b'; break; + case '\f': *ptr2++='f'; break; + case '\n': *ptr2++='n'; break; + case '\r': *ptr2++='r'; break; + case '\t': *ptr2++='t'; break; + default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */ + } + } + } + *ptr2++='\"';*ptr2++=0; + return out; +} +/* Invote print_string_ptr (which is useful) on an item. */ +static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);} + +/* Predeclare these prototypes. */ +static const char *parse_value(cJSON *item,const char *value); +static char *print_value(cJSON *item,int depth,int fmt); +static const char *parse_array(cJSON *item,const char *value); +static char *print_array(cJSON *item,int depth,int fmt); +static const char *parse_object(cJSON *item,const char *value); +static char *print_object(cJSON *item,int depth,int fmt); + +/* Utility to jump whitespace and cr/lf */ +static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} + +/* Parse an object - create a new root, and populate. */ +cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated) +{ + const char *end=0; + cJSON *c=cJSON_New_Item(); + ep=0; + if (!c) return 0; /* memory fail */ + + end=parse_value(c,skip(value)); + if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */ + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}} + if (return_parse_end) *return_parse_end=end; + return c; +} +/* Default options for cJSON_Parse */ +cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} + +/* Render a cJSON item/entity/structure to text. */ +char *cJSON_Print(cJSON *item) {return print_value(item,0,1);} +char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);} + +/* Parser core - when encountering text, process appropriately. */ +static const char *parse_value(cJSON *item,const char *value) +{ + if (!value) return 0; /* Fail on null. */ + if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; } + if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; } + if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; } + if (*value=='\"') { return parse_string(item,value); } + if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); } + if (*value=='[') { return parse_array(item,value); } + if (*value=='{') { return parse_object(item,value); } + + ep=value;return 0; /* failure. */ +} + +/* Render a value to text. */ +static char *print_value(cJSON *item,int depth,int fmt) +{ + char *out=0; + if (!item) return 0; + switch ((item->type)&255) + { + case cJSON_NULL: out=cJSON_strdup("null"); break; + case cJSON_False: out=cJSON_strdup("false");break; + case cJSON_True: out=cJSON_strdup("true"); break; + case cJSON_Number: out=print_number(item);break; + case cJSON_String: out=print_string(item);break; + case cJSON_Array: out=print_array(item,depth,fmt);break; + case cJSON_Object: out=print_object(item,depth,fmt);break; + } + return out; +} + +/* Build an array from input text. */ +static const char *parse_array(cJSON *item,const char *value) +{ + cJSON *child; + if (*value!='[') {ep=value;return 0;} /* not an array! */ + + item->type=cJSON_Array; + value=skip(value+1); + if (*value==']') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; /* memory fail */ + value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_value(child,skip(value+1))); + if (!value) return 0; /* memory fail */ + } + + if (*value==']') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an array to text */ +static char *print_array(cJSON *item,int depth,int fmt) +{ + char **entries; + char *out=0,*ptr,*ret;int len=5; + cJSON *child=item->child; + int numentries=0,i=0,fail=0; + + /* How many entries in the array? */ + while (child) numentries++,child=child->next; + /* Explicitly handle numentries==0 */ + if (!numentries) + { + out=(char*)cJSON_malloc(3); + if (out) strcpy(out,"[]"); + return out; + } + /* Allocate an array to hold the values for each */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + memset(entries,0,numentries*sizeof(char*)); + /* Retrieve all the results: */ + child=item->child; + while (child && !fail) + { + ret=print_value(child,depth+1,fmt); + entries[i++]=ret; + if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; + child=child->next; + } + + /* If we didn't fail, try to malloc the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + /* If that fails, we fail. */ + if (!out) fail=1; + + /* Handle failure. */ + if (fail) + { + for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]); + cJSON_free(entries); + return 0; + } + + /* Compose the output array. */ + *out='['; + ptr=out+1;*ptr=0; + for (i=0;i<numentries;i++) + { + strcpy(ptr,entries[i]);ptr+=strlen(entries[i]); + if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;} + cJSON_free(entries[i]); + } + cJSON_free(entries); + *ptr++=']';*ptr++=0; + return out; +} + +/* Build an object from the text. */ +static const char *parse_object(cJSON *item,const char *value) +{ + cJSON *child; + if (*value!='{') {ep=value;return 0;} /* not an object! */ + + item->type=cJSON_Object; + value=skip(value+1); + if (*value=='}') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; + value=skip(parse_string(child,skip(value))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_string(child,skip(value+1))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + } + + if (*value=='}') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an object to text. */ +static char *print_object(cJSON *item,int depth,int fmt) +{ + char **entries=0,**names=0; + char *out=0,*ptr,*ret,*str;int len=7,i=0,j; + cJSON *child=item->child; + int numentries=0,fail=0; + /* Count the number of entries. */ + while (child) numentries++,child=child->next; + /* Explicitly handle empty object case */ + if (!numentries) + { + out=(char*)cJSON_malloc(fmt?depth+4:3); + if (!out) return 0; + ptr=out;*ptr++='{'; + if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';} + *ptr++='}';*ptr++=0; + return out; + } + /* Allocate space for the names and the objects */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + names=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!names) {cJSON_free(entries);return 0;} + memset(entries,0,sizeof(char*)*numentries); + memset(names,0,sizeof(char*)*numentries); + + /* Collect all the results into our arrays: */ + child=item->child;depth++;if (fmt) len+=depth; + while (child) + { + names[i]=str=print_string_ptr(child->string); + entries[i++]=ret=print_value(child,depth,fmt); + if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; + child=child->next; + } + + /* Try to allocate the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + if (!out) fail=1; + + /* Handle failure */ + if (fail) + { + for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);} + cJSON_free(names);cJSON_free(entries); + return 0; + } + + /* Compose the output: */ + *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0; + for (i=0;i<numentries;i++) + { + if (fmt) for (j=0;j<depth;j++) *ptr++='\t'; + strcpy(ptr,names[i]);ptr+=strlen(names[i]); + *ptr++=':';if (fmt) *ptr++='\t'; + strcpy(ptr,entries[i]);ptr+=strlen(entries[i]); + if (i!=numentries-1) *ptr++=','; + if (fmt) *ptr++='\n';*ptr=0; + cJSON_free(names[i]);cJSON_free(entries[i]); + } + + cJSON_free(names);cJSON_free(entries); + if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t'; + *ptr++='}';*ptr++=0; + return out; +} + +/* Get Array size/item / object item. */ +int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;} +cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;} +cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} +/* Utility for handling references. */ +static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;} + +/* Add item to array/object. */ +void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}} +void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} +void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));} +void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));} + +cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0; + if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;} +void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));} +cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;} +void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} + +/* Replace array/object items with new ones. */ +void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; + newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem; + if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);} +void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}} + +/* Create basic types: */ +cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;} +cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;} +cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;} +cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;} +cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;} +cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;} +cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;} +cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;} + +/* Create Arrays: */ +cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;} + +/* Duplication */ +cJSON *cJSON_Duplicate(cJSON *item,int recurse) +{ + cJSON *newitem,*cptr,*nptr=0,*newchild; + /* Bail on bad ptr */ + if (!item) return 0; + /* Create new item */ + newitem=cJSON_New_Item(); + if (!newitem) return 0; + /* Copy over all vars */ + newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; + if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} + if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}} + /* If non-recursive, then we're done! */ + if (!recurse) return newitem; + /* Walk the ->next chain for the child. */ + cptr=item->child; + while (cptr) + { + newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) {cJSON_Delete(newitem);return 0;} + if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ + cptr=cptr->next; + } + return newitem; +} + +void cJSON_Minify(char *json) +{ + char *into=json; + while (*json) + { + if (*json==' ') json++; + else if (*json=='\t') json++; // Whitespace characters. + else if (*json=='\r') json++; + else if (*json=='\n') json++; + else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line. + else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments. + else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive. + else *into++=*json++; // All other characters. + } + *into=0; // and null-terminate. +} \ No newline at end of file diff --git a/3rdparty/cjson/cJSON.h b/3rdparty/cjson/cJSON.h new file mode 100644 index 000000000..867b7c32f --- /dev/null +++ b/3rdparty/cjson/cJSON.h @@ -0,0 +1,143 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + 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 + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* cJSON Types: */ +#define cJSON_False 0 +#define cJSON_True 1 +#define cJSON_NULL 2 +#define cJSON_Number 3 +#define cJSON_String 4 +#define cJSON_Array 5 +#define cJSON_Object 6 + +#define cJSON_IsReference 256 + +/* The cJSON structure: */ +typedef struct cJSON { + struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + + int type; /* The type of the item, as above. */ + + char *valuestring; /* The item's string, if type==cJSON_String */ + int valueint; /* The item's number, if type==cJSON_Number */ + double valuedouble; /* The item's number, if type==cJSON_Number */ + + char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ +} cJSON; + +typedef struct cJSON_Hooks { + void *(*malloc_fn)(size_t sz); + void (*free_fn)(void *ptr); +} cJSON_Hooks; + +/* Supply malloc, realloc and free functions to cJSON */ +extern void cJSON_InitHooks(cJSON_Hooks* hooks); + + +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ +extern cJSON *cJSON_Parse(const char *value); +/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ +extern char *cJSON_Print(cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ +extern char *cJSON_PrintUnformatted(cJSON *item); +/* Delete a cJSON entity and all subentities. */ +extern void cJSON_Delete(cJSON *c); + +/* Returns the number of items in an array (or object). */ +extern int cJSON_GetArraySize(cJSON *array); +/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ +extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); +/* Get item "string" from object. Case insensitive. */ +extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); + +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +extern const char *cJSON_GetErrorPtr(void); + +/* These calls create a cJSON item of the appropriate type. */ +extern cJSON *cJSON_CreateNull(void); +extern cJSON *cJSON_CreateTrue(void); +extern cJSON *cJSON_CreateFalse(void); +extern cJSON *cJSON_CreateBool(int b); +extern cJSON *cJSON_CreateNumber(double num); +extern cJSON *cJSON_CreateString(const char *string); +extern cJSON *cJSON_CreateArray(void); +extern cJSON *cJSON_CreateObject(void); + +/* These utilities create an Array of count items. */ +extern cJSON *cJSON_CreateIntArray(const int *numbers,int count); +extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count); +extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count); +extern cJSON *cJSON_CreateStringArray(const char **strings,int count); + +/* Append item to the specified array/object. */ +extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); +extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); + +/* Remove/Detatch items from Arrays/Objects. */ +extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which); +extern void cJSON_DeleteItemFromArray(cJSON *array,int which); +extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); +extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); + +/* Update array items. */ +extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); +extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +extern cJSON *cJSON_Duplicate(cJSON *item,int recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will +need to be released. With recurse!=0, it will duplicate any children connected to the item. +The item->next and ->prev pointers are always zero on return from Duplicate. */ + +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated); + +extern void cJSON_Minify(char *json); + +/* Macros for creating things quickly. */ +#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) +#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) +#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) +#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) +#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) +#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/3rdparty/cjson/test.c b/3rdparty/cjson/test.c new file mode 100644 index 000000000..b308a92c8 --- /dev/null +++ b/3rdparty/cjson/test.c @@ -0,0 +1,156 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + 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 + THE SOFTWARE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include "cJSON.h" + +/* Parse text to JSON, then render back to text, and print! */ +void doit(char *text) +{ + char *out;cJSON *json; + + json=cJSON_Parse(text); + if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());} + else + { + out=cJSON_Print(json); + cJSON_Delete(json); + printf("%s\n",out); + free(out); + } +} + +/* Read a file, parse, render back, etc. */ +void dofile(char *filename) +{ + FILE *f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET); + char *data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f); + doit(data); + free(data); +} + +/* Used by some code below as an example datatype. */ +struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; }; + +/* Create a bunch of objects as demonstration. */ +void create_objects() +{ + cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */ + + /* Here we construct some JSON standards, from the JSON site. */ + + /* Our "Video" datatype: */ + root=cJSON_CreateObject(); + cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); + cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject()); + cJSON_AddStringToObject(fmt,"type", "rect"); + cJSON_AddNumberToObject(fmt,"width", 1920); + cJSON_AddNumberToObject(fmt,"height", 1080); + cJSON_AddFalseToObject (fmt,"interlace"); + cJSON_AddNumberToObject(fmt,"frame rate", 24); + + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */ + + /* Our "days of the week" array: */ + const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; + root=cJSON_CreateStringArray(strings,7); + + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); + + /* Our matrix: */ + int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}}; + root=cJSON_CreateArray(); + for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3)); + +/* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */ + + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); + + + /* Our "gallery" item: */ + int ids[4]={116,943,234,38793}; + root=cJSON_CreateObject(); + cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject()); + cJSON_AddNumberToObject(img,"Width",800); + cJSON_AddNumberToObject(img,"Height",600); + cJSON_AddStringToObject(img,"Title","View from 15th Floor"); + cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject()); + cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943"); + cJSON_AddNumberToObject(thm,"Height",125); + cJSON_AddStringToObject(thm,"Width","100"); + cJSON_AddItemToObject(img,"IDs", cJSON_CreateIntArray(ids,4)); + + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); + + /* Our array of "records": */ + struct record fields[2]={ + {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"}, + {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}}; + + root=cJSON_CreateArray(); + for (i=0;i<2;i++) + { + cJSON_AddItemToArray(root,fld=cJSON_CreateObject()); + cJSON_AddStringToObject(fld, "precision", fields[i].precision); + cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat); + cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon); + cJSON_AddStringToObject(fld, "Address", fields[i].address); + cJSON_AddStringToObject(fld, "City", fields[i].city); + cJSON_AddStringToObject(fld, "State", fields[i].state); + cJSON_AddStringToObject(fld, "Zip", fields[i].zip); + cJSON_AddStringToObject(fld, "Country", fields[i].country); + } + +/* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */ + + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); + +} + +int main (int argc, const char * argv[]) { + /* a bunch of json: */ + char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\": \"rect\", \n\"width\": 1920, \n\"height\": 1080, \n\"interlace\": false,\"frame rate\": 24\n}\n}"; + char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]"; + char text3[]="[\n [0, -1, 0],\n [1, 0, 0],\n [0, 0, 1]\n ]\n"; + char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http:/*www.example.com/image/481989943\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }"; + char text5[]="[\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.7668,\n \"Longitude\": -122.3959,\n \"Address\": \"\",\n \"City\": \"SAN FRANCISCO\",\n \"State\": \"CA\",\n \"Zip\": \"94107\",\n \"Country\": \"US\"\n },\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.371991,\n \"Longitude\": -122.026020,\n \"Address\": \"\",\n \"City\": \"SUNNYVALE\",\n \"State\": \"CA\",\n \"Zip\": \"94085\",\n \"Country\": \"US\"\n }\n ]"; + + /* Process each json textblock by parsing, then rebuilding: */ + doit(text1); + doit(text2); + doit(text3); + doit(text4); + doit(text5); + + /* Parse standard testfiles: */ +/* dofile("../../tests/test1"); */ +/* dofile("../../tests/test2"); */ +/* dofile("../../tests/test3"); */ +/* dofile("../../tests/test4"); */ +/* dofile("../../tests/test5"); */ + + /* Now some samplecode for building objects concisely: */ + create_objects(); + + return 0; +} diff --git a/3rdparty/cjson/tests/test1 b/3rdparty/cjson/tests/test1 new file mode 100644 index 000000000..eacfbf5e6 --- /dev/null +++ b/3rdparty/cjson/tests/test1 @@ -0,0 +1,22 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } +} diff --git a/3rdparty/cjson/tests/test2 b/3rdparty/cjson/tests/test2 new file mode 100644 index 000000000..5600991a4 --- /dev/null +++ b/3rdparty/cjson/tests/test2 @@ -0,0 +1,11 @@ +{"menu": { + "id": "file", + "value": "File", + "popup": { + "menuitem": [ + {"value": "New", "onclick": "CreateNewDoc()"}, + {"value": "Open", "onclick": "OpenDoc()"}, + {"value": "Close", "onclick": "CloseDoc()"} + ] + } +}} diff --git a/3rdparty/cjson/tests/test3 b/3rdparty/cjson/tests/test3 new file mode 100644 index 000000000..5662b3774 --- /dev/null +++ b/3rdparty/cjson/tests/test3 @@ -0,0 +1,26 @@ +{"widget": { + "debug": "on", + "window": { + "title": "Sample Konfabulator Widget", + "name": "main_window", + "width": 500, + "height": 500 + }, + "image": { + "src": "Images/Sun.png", + "name": "sun1", + "hOffset": 250, + "vOffset": 250, + "alignment": "center" + }, + "text": { + "data": "Click Here", + "size": 36, + "style": "bold", + "name": "text1", + "hOffset": 250, + "vOffset": 100, + "alignment": "center", + "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" + } +}} \ No newline at end of file diff --git a/3rdparty/cjson/tests/test4 b/3rdparty/cjson/tests/test4 new file mode 100644 index 000000000..d540b57f0 --- /dev/null +++ b/3rdparty/cjson/tests/test4 @@ -0,0 +1,88 @@ +{"web-app": { + "servlet": [ + { + "servlet-name": "cofaxCDS", + "servlet-class": "org.cofax.cds.CDSServlet", + "init-param": { + "configGlossary:installationAt": "Philadelphia, PA", + "configGlossary:adminEmail": "ksm@pobox.com", + "configGlossary:poweredBy": "Cofax", + "configGlossary:poweredByIcon": "/images/cofax.gif", + "configGlossary:staticPath": "/content/static", + "templateProcessorClass": "org.cofax.WysiwygTemplate", + "templateLoaderClass": "org.cofax.FilesTemplateLoader", + "templatePath": "templates", + "templateOverridePath": "", + "defaultListTemplate": "listTemplate.htm", + "defaultFileTemplate": "articleTemplate.htm", + "useJSP": false, + "jspListTemplate": "listTemplate.jsp", + "jspFileTemplate": "articleTemplate.jsp", + "cachePackageTagsTrack": 200, + "cachePackageTagsStore": 200, + "cachePackageTagsRefresh": 60, + "cacheTemplatesTrack": 100, + "cacheTemplatesStore": 50, + "cacheTemplatesRefresh": 15, + "cachePagesTrack": 200, + "cachePagesStore": 100, + "cachePagesRefresh": 10, + "cachePagesDirtyRead": 10, + "searchEngineListTemplate": "forSearchEnginesList.htm", + "searchEngineFileTemplate": "forSearchEngines.htm", + "searchEngineRobotsDb": "WEB-INF/robots.db", + "useDataStore": true, + "dataStoreClass": "org.cofax.SqlDataStore", + "redirectionClass": "org.cofax.SqlRedirection", + "dataStoreName": "cofax", + "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", + "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", + "dataStoreUser": "sa", + "dataStorePassword": "dataStoreTestQuery", + "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", + "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", + "dataStoreInitConns": 10, + "dataStoreMaxConns": 100, + "dataStoreConnUsageLimit": 100, + "dataStoreLogLevel": "debug", + "maxUrlLength": 500}}, + { + "servlet-name": "cofaxEmail", + "servlet-class": "org.cofax.cds.EmailServlet", + "init-param": { + "mailHost": "mail1", + "mailHostOverride": "mail2"}}, + { + "servlet-name": "cofaxAdmin", + "servlet-class": "org.cofax.cds.AdminServlet"}, + + { + "servlet-name": "fileServlet", + "servlet-class": "org.cofax.cds.FileServlet"}, + { + "servlet-name": "cofaxTools", + "servlet-class": "org.cofax.cms.CofaxToolsServlet", + "init-param": { + "templatePath": "toolstemplates/", + "log": 1, + "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", + "logMaxSize": "", + "dataLog": 1, + "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", + "dataLogMaxSize": "", + "removePageCache": "/content/admin/remove?cache=pages&id=", + "removeTemplateCache": "/content/admin/remove?cache=templates&id=", + "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", + "lookInContext": 1, + "adminGroupID": 4, + "betaServer": true}}], + "servlet-mapping": { + "cofaxCDS": "/", + "cofaxEmail": "/cofaxutil/aemail/*", + "cofaxAdmin": "/admin/*", + "fileServlet": "/static/*", + "cofaxTools": "/tools/*"}, + + "taglib": { + "taglib-uri": "cofax.tld", + "taglib-location": "/WEB-INF/tlds/cofax.tld"}}} \ No newline at end of file diff --git a/3rdparty/cjson/tests/test5 b/3rdparty/cjson/tests/test5 new file mode 100644 index 000000000..49980ca25 --- /dev/null +++ b/3rdparty/cjson/tests/test5 @@ -0,0 +1,27 @@ +{"menu": { + "header": "SVG Viewer", + "items": [ + {"id": "Open"}, + {"id": "OpenNew", "label": "Open New"}, + null, + {"id": "ZoomIn", "label": "Zoom In"}, + {"id": "ZoomOut", "label": "Zoom Out"}, + {"id": "OriginalView", "label": "Original View"}, + null, + {"id": "Quality"}, + {"id": "Pause"}, + {"id": "Mute"}, + null, + {"id": "Find", "label": "Find..."}, + {"id": "FindAgain", "label": "Find Again"}, + {"id": "Copy"}, + {"id": "CopyAgain", "label": "Copy Again"}, + {"id": "CopySVG", "label": "Copy SVG"}, + {"id": "ViewSVG", "label": "View SVG"}, + {"id": "ViewSource", "label": "View Source"}, + {"id": "SaveAs", "label": "Save As"}, + null, + {"id": "Help"}, + {"id": "About", "label": "About Adobe CVG Viewer..."} + ] +}} diff --git a/3rdparty/mongoose/CMakeLists.txt b/3rdparty/mongoose/CMakeLists.txt new file mode 100644 index 000000000..26182797d --- /dev/null +++ b/3rdparty/mongoose/CMakeLists.txt @@ -0,0 +1,11 @@ +include(FlightGearComponent) + +set(SOURCES + mongoose.c + ) + +set(HEADERS + mongoose.h + ) + +flightgear_component(Mongoose "${SOURCES}" "${HEADERS}") diff --git a/3rdparty/mongoose/LICENSE b/3rdparty/mongoose/LICENSE new file mode 100644 index 000000000..2473d6dd7 --- /dev/null +++ b/3rdparty/mongoose/LICENSE @@ -0,0 +1,16 @@ +Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> +Copyright (c) 2013 Cesanta Software Limited +All rights reserved + +This code is dual-licensed: you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2 as +published by the Free Software Foundation. For the terms of this +license, see http://www.gnu.org/licenses. + +You are free to use this code under the terms of the GNU General +Public License, but WITHOUT ANY WARRANTY; without even the implied +warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License for more details. + +Alternatively, you can license this code under a commercial +license, as set out in http://cesanta.com/products.html. diff --git a/3rdparty/mongoose/mongoose.c b/3rdparty/mongoose/mongoose.c new file mode 100644 index 000000000..8ee46058c --- /dev/null +++ b/3rdparty/mongoose/mongoose.c @@ -0,0 +1,4211 @@ +// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> +// Copyright (c) 2013-2014 Cesanta Software Limited +// All rights reserved +// +// This library is dual-licensed: you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. For the terms of this +// license, see <http://www.gnu.org/licenses/>. +// +// You are free to use this library under the terms of the GNU General +// Public License, but WITHOUT ANY WARRANTY; without even the implied +// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// Alternatively, you can license this library under a commercial +// license, as set out in <http://cesanta.com/>. + +#undef UNICODE // Use ANSI WinAPI functions +#undef _UNICODE // Use multibyte encoding on Windows +#define _MBCS // Use multibyte encoding on Windows +#define _INTEGRAL_MAX_BITS 64 // Enable _stati64() on Windows +#define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005+ +#undef WIN32_LEAN_AND_MEAN // Let windows.h always include winsock2.h +#define _XOPEN_SOURCE 600 // For flockfile() on Linux +#define __STDC_FORMAT_MACROS // <inttypes.h> wants this for C++ +#define __STDC_LIMIT_MACROS // C++ wants that for INT64_MAX +#define _LARGEFILE_SOURCE // Enable fseeko() and ftello() functions +#define _FILE_OFFSET_BITS 64 // Enable 64-bit file offsets + +#ifdef _MSC_VER +#pragma warning (disable : 4127) // FD_SET() emits warning, disable it +#pragma warning (disable : 4204) // missing c99 support +#endif + +#include <sys/types.h> +#include <sys/stat.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <assert.h> +#include <errno.h> +#include <time.h> +#include <ctype.h> +#include <stdarg.h> + +#ifdef _WIN32 +#include <windows.h> +#include <process.h> // For _beginthread +#include <io.h> // For _lseeki64 +#include <direct.h> // For _mkdir +typedef int socklen_t; +#if !defined(__MINGW32__) || !defined(_PID_T_) || defined(_NO_OLDNAMES) +typedef HANDLE pid_t; +#endif +typedef SOCKET sock_t; +typedef unsigned char uint8_t; +typedef unsigned int uint32_t; +typedef unsigned short uint16_t; +typedef unsigned __int64 uint64_t; +typedef __int64 int64_t; +typedef CRITICAL_SECTION mutex_t; +typedef struct _stati64 file_stat_t; +#pragma comment(lib, "ws2_32.lib") +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#define INT64_FMT "I64d" +#ifndef EINPROGRESS +#define EINPROGRESS WSAEINPROGRESS +#endif +#ifndef EWOULDBLOCK +#define EWOULDBLOCK WSAEWOULDBLOCK +#endif +#define mutex_init(x) InitializeCriticalSection(x) +#define mutex_destroy(x) DeleteCriticalSection(x) +#define mutex_lock(x) EnterCriticalSection(x) +#define mutex_unlock(x) LeaveCriticalSection(x) +#define get_thread_id() ((unsigned long) GetCurrentThreadId()) +#ifndef S_ISDIR +#define S_ISDIR(x) ((x) & _S_IFDIR) +#endif +#define sleep(x) Sleep((x) * 1000) +#define stat(x, y) mg_stat((x), (y)) +#define fopen(x, y) mg_fopen((x), (y)) +#define open(x, y) mg_open((x), (y)) +#define lseek(x, y, z) _lseeki64((x), (y), (z)) +#define mkdir(x, y) _mkdir(x) +#define to64(x) _atoi64(x) +#define flockfile(x) +#define funlockfile(x) +#ifndef va_copy +#define va_copy(x,y) x = y +#endif // MINGW #defines va_copy +#ifndef __func__ +#define STRX(x) #x +#define STR(x) STRX(x) +#define __func__ __FILE__ ":" STR(__LINE__) +#endif +#else +#include <dirent.h> +#include <inttypes.h> +#include <pthread.h> +#include <pwd.h> +#include <signal.h> +#include <unistd.h> +#include <netdb.h> +#include <arpa/inet.h> // For inet_pton() when MONGOOSE_USE_IPV6 is defined +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/select.h> +#define closesocket(x) close(x) +typedef int sock_t; +typedef pthread_mutex_t mutex_t; +typedef struct stat file_stat_t; +#define mutex_init(x) pthread_mutex_init(x, NULL) +#define mutex_destroy(x) pthread_mutex_destroy(x) +#define mutex_lock(x) pthread_mutex_lock(x) +#define mutex_unlock(x) pthread_mutex_unlock(x) +#define get_thread_id() ((unsigned long) pthread_self()) +#define INVALID_SOCKET ((sock_t) -1) +#define INT64_FMT PRId64 +#define to64(x) strtoll(x, NULL, 10) +#define __cdecl +#define O_BINARY 0 +#endif + +#ifdef MONGOOSE_USE_SSL +#ifdef __APPLE__ +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif +#include <openssl/ssl.h> +#endif + +#include "mongoose.h" + +struct ll { struct ll *prev, *next; }; +#define LINKED_LIST_INIT(N) ((N)->next = (N)->prev = (N)) +#define LINKED_LIST_DECLARE_AND_INIT(H) struct ll H = { &H, &H } +#define LINKED_LIST_ENTRY(P,T,N) ((T *)((char *)(P) - offsetof(T, N))) +#define LINKED_LIST_IS_EMPTY(N) ((N)->next == (N)) +#define LINKED_LIST_FOREACH(H,N,T) \ + for (N = (H)->next, T = (N)->next; N != (H); N = (T), T = (N)->next) +#define LINKED_LIST_ADD_TO_FRONT(H,N) do { ((H)->next)->prev = (N); \ + (N)->next = ((H)->next); (N)->prev = (H); (H)->next = (N); } while (0) +#define LINKED_LIST_ADD_TO_TAIL(H,N) do { ((H)->prev)->next = (N); \ + (N)->prev = ((H)->prev); (N)->next = (H); (H)->prev = (N); } while (0) +#define LINKED_LIST_REMOVE(N) do { ((N)->next)->prev = ((N)->prev); \ + ((N)->prev)->next = ((N)->next); LINKED_LIST_INIT(N); } while (0) + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) +#define MAX_REQUEST_SIZE 16384 +#define IOBUF_SIZE 8192 +#define MAX_PATH_SIZE 8192 +#define LUA_SCRIPT_PATTERN "**.lp$" +#define DEFAULT_CGI_PATTERN "**.cgi$|**.pl$|**.php$" +#define CGI_ENVIRONMENT_SIZE 4096 +#define MAX_CGI_ENVIR_VARS 64 +#define ENV_EXPORT_TO_CGI "MONGOOSE_CGI" +#define PASSWORDS_FILE_NAME ".htpasswd" + +#ifndef MONGOOSE_USE_WEBSOCKET_PING_INTERVAL +#define MONGOOSE_USE_WEBSOCKET_PING_INTERVAL 5 +#endif + +// Extra HTTP headers to send in every static file reply +#if !defined(MONGOOSE_USE_EXTRA_HTTP_HEADERS) +#define MONGOOSE_USE_EXTRA_HTTP_HEADERS "" +#endif + +#ifndef MONGOOSE_USE_POST_SIZE_LIMIT +#define MONGOOSE_USE_POST_SIZE_LIMIT 0 +#endif + +#ifndef MONGOOSE_USE_IDLE_TIMEOUT_SECONDS +#define MONGOOSE_USE_IDLE_TIMEOUT_SECONDS 30 +#endif + +#ifdef MONGOOSE_ENABLE_DEBUG +#define DBG(x) do { printf("%-20s ", __func__); printf x; putchar('\n'); \ + fflush(stdout); } while(0) +#else +#define DBG(x) +#endif + +#ifdef MONGOOSE_NO_FILESYSTEM +#define MONGOOSE_NO_AUTH +#define MONGOOSE_NO_CGI +#define MONGOOSE_NO_DAV +#define MONGOOSE_NO_DIRECTORY_LISTING +#define MONGOOSE_NO_LOGGING +#endif + +union socket_address { + struct sockaddr sa; + struct sockaddr_in sin; +#ifdef MONGOOSE_USE_IPV6 + struct sockaddr_in6 sin6; +#endif +}; + +struct vec { + const char *ptr; + int len; +}; + +// For directory listing and WevDAV support +struct dir_entry { + struct connection *conn; + char *file_name; + file_stat_t st; +}; + +// NOTE(lsm): this enum shoulds be in sync with the config_options. +enum { + ACCESS_CONTROL_LIST, +#ifndef MONGOOSE_NO_FILESYSTEM + ACCESS_LOG_FILE, +#ifndef MONGOOSE_NO_AUTH + AUTH_DOMAIN, +#endif +#ifndef MONGOOSE_NO_CGI + CGI_INTERPRETER, + CGI_PATTERN, +#endif +#ifndef MONGOOSE_NO_DAV + DAV_AUTH_FILE, +#endif + DOCUMENT_ROOT, +#ifndef MONGOOSE_NO_DIRECTORY_LISTING + ENABLE_DIRECTORY_LISTING, +#endif +#endif + EXTRA_MIME_TYPES, +#if !defined(MONGOOSE_NO_FILESYSTEM) && !defined(MONGOOSE_NO_AUTH) + GLOBAL_AUTH_FILE, +#endif + HIDE_FILES_PATTERN, +#ifndef MONGOOSE_NO_FILESYSTEM + INDEX_FILES, +#endif + LISTENING_PORT, +#ifndef _WIN32 + RUN_AS_USER, +#endif +#ifdef MONGOOSE_USE_SSL + SSL_CERTIFICATE, +#endif + URL_REWRITES, + NUM_OPTIONS +}; + +static const char *static_config_options[] = { + "access_control_list", NULL, +#ifndef MONGOOSE_NO_FILESYSTEM + "access_log_file", NULL, +#ifndef MONGOOSE_NO_AUTH + "auth_domain", "mydomain.com", +#endif +#ifndef MONGOOSE_NO_CGI + "cgi_interpreter", NULL, + "cgi_pattern", DEFAULT_CGI_PATTERN, +#endif +#ifndef MONGOOSE_NO_DAV + "dav_auth_file", NULL, +#endif + "document_root", NULL, +#ifndef MONGOOSE_NO_DIRECTORY_LISTING + "enable_directory_listing", "yes", +#endif +#endif + "extra_mime_types", NULL, +#if !defined(MONGOOSE_NO_FILESYSTEM) && !defined(MONGOOSE_NO_AUTH) + "global_auth_file", NULL, +#endif + "hide_files_patterns", NULL, +#ifndef MONGOOSE_NO_FILESYSTEM + "index_files","index.html,index.htm,index.cgi,index.php,index.lp", +#endif + "listening_port", NULL, +#ifndef _WIN32 + "run_as_user", NULL, +#endif +#ifdef MONGOOSE_USE_SSL + "ssl_certificate", NULL, +#endif + "url_rewrites", NULL, + NULL +}; + +struct mg_server { + sock_t listening_sock; + union socket_address lsa; // Listening socket address + struct ll active_connections; + mg_handler_t request_handler; + mg_handler_t error_handler; + mg_handler_t auth_handler; + char *config_options[NUM_OPTIONS]; + char local_ip[48]; + void *server_data; +#ifdef MONGOOSE_USE_SSL + SSL_CTX *ssl_ctx; // Server SSL context + SSL_CTX *client_ssl_ctx; // Client SSL context +#endif + sock_t ctl[2]; // Control socketpair. Used to wake up from select() call +}; + +// Expandable IO buffer +struct iobuf { + char *buf; // Buffer that holds the data + int size; // Buffer size + int len; // Number of bytes currently in a buffer +}; + +// Local endpoint representation +union endpoint { + int fd; // Opened regular local file + sock_t cgi_sock; // CGI socket + void *ssl; // SSL descriptor +}; + +enum endpoint_type { EP_NONE, EP_FILE, EP_CGI, EP_USER, EP_PUT, EP_CLIENT }; +enum connection_flags { + CONN_CLOSE = 1, // Connection must be closed at the end of the poll + CONN_SPOOL_DONE = 2, // All data has been buffered for sending + CONN_SSL_HANDS_SHAKEN = 4, // SSL handshake has completed. Only for SSL + CONN_HEADERS_SENT = 8, // User callback has sent HTTP headers + CONN_BUFFER = 16, // CGI only. Holds data send until CGI prints + // all HTTP headers + CONN_CONNECTING = 32, // HTTP client is doing non-blocking connect() + CONN_LONG_RUNNING = 64 // Long-running URI handlers +}; + +struct connection { + struct mg_connection mg_conn; // XXX: Must be first + struct ll link; // Linkage to server->active_connections + struct mg_server *server; + sock_t client_sock; // Connected client + struct iobuf local_iobuf; + struct iobuf remote_iobuf; + union endpoint endpoint; + enum endpoint_type endpoint_type; + time_t birth_time; + time_t last_activity_time; + char *path_info; + char *request; + int64_t num_bytes_sent; // Total number of bytes sent + int64_t cl; // Reply content length, for Range support + int request_len; // Request length, including last \r\n after last header + int flags; // CONN_* flags: CONN_CLOSE, CONN_SPOOL_DONE, etc + mg_handler_t handler; // Callback for HTTP client +#ifdef MONGOOSE_USE_SSL + SSL *ssl; // SSL descriptor +#endif +}; + +static void open_local_endpoint(struct connection *conn, int skip_user); +static void close_local_endpoint(struct connection *conn); + +static const struct { + const char *extension; + size_t ext_len; + const char *mime_type; +} static_builtin_mime_types[] = { + {".html", 5, "text/html"}, + {".htm", 4, "text/html"}, + {".shtm", 5, "text/html"}, + {".shtml", 6, "text/html"}, + {".css", 4, "text/css"}, + {".js", 3, "application/x-javascript"}, + {".ico", 4, "image/x-icon"}, + {".gif", 4, "image/gif"}, + {".jpg", 4, "image/jpeg"}, + {".jpeg", 5, "image/jpeg"}, + {".png", 4, "image/png"}, + {".svg", 4, "image/svg+xml"}, + {".txt", 4, "text/plain"}, + {".torrent", 8, "application/x-bittorrent"}, + {".wav", 4, "audio/x-wav"}, + {".mp3", 4, "audio/x-mp3"}, + {".mid", 4, "audio/mid"}, + {".m3u", 4, "audio/x-mpegurl"}, + {".ogg", 4, "application/ogg"}, + {".ram", 4, "audio/x-pn-realaudio"}, + {".xml", 4, "text/xml"}, + {".json", 5, "text/json"}, + {".xslt", 5, "application/xml"}, + {".xsl", 4, "application/xml"}, + {".ra", 3, "audio/x-pn-realaudio"}, + {".doc", 4, "application/msword"}, + {".exe", 4, "application/octet-stream"}, + {".zip", 4, "application/x-zip-compressed"}, + {".xls", 4, "application/excel"}, + {".tgz", 4, "application/x-tar-gz"}, + {".tar", 4, "application/x-tar"}, + {".gz", 3, "application/x-gunzip"}, + {".arj", 4, "application/x-arj-compressed"}, + {".rar", 4, "application/x-arj-compressed"}, + {".rtf", 4, "application/rtf"}, + {".pdf", 4, "application/pdf"}, + {".swf", 4, "application/x-shockwave-flash"}, + {".mpg", 4, "video/mpeg"}, + {".webm", 5, "video/webm"}, + {".mpeg", 5, "video/mpeg"}, + {".mov", 4, "video/quicktime"}, + {".mp4", 4, "video/mp4"}, + {".m4v", 4, "video/x-m4v"}, + {".asf", 4, "video/x-ms-asf"}, + {".avi", 4, "video/x-msvideo"}, + {".bmp", 4, "image/bmp"}, + {".ttf", 4, "application/x-font-ttf"}, + {NULL, 0, NULL} +}; + +#ifndef MONGOOSE_NO_THREADS +void *mg_start_thread(void *(*f)(void *), void *p) { +#ifdef _WIN32 + return (void *) _beginthread((void (__cdecl *)(void *)) f, 0, p); +#else + pthread_t thread_id = (pthread_t) 0; + pthread_attr_t attr; + + (void) pthread_attr_init(&attr); + (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + +#if MONGOOSE_USE_STACK_SIZE > 1 + (void) pthread_attr_setstacksize(&attr, MONGOOSE_USE_STACK_SIZE); +#endif + + pthread_create(&thread_id, &attr, f, p); + pthread_attr_destroy(&attr); + + return (void *) thread_id; +#endif +} +#endif // MONGOOSE_NO_THREADS + +#ifdef _WIN32 +// Encode 'path' which is assumed UTF-8 string, into UNICODE string. +// wbuf and wbuf_len is a target buffer and its length. +static void to_wchar(const char *path, wchar_t *wbuf, size_t wbuf_len) { + char buf[MAX_PATH_SIZE * 2], buf2[MAX_PATH_SIZE * 2], *p; + + strncpy(buf, path, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + + // Trim trailing slashes + p = buf + strlen(buf) - 1; + while (p > buf && p[0] == '\\' || p[0] == '/') *p-- = '\0'; + //change_slashes_to_backslashes(buf); + + // Convert to Unicode and back. If doubly-converted string does not + // match the original, something is fishy, reject. + memset(wbuf, 0, wbuf_len * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, (int) wbuf_len); + WideCharToMultiByte(CP_UTF8, 0, wbuf, (int) wbuf_len, buf2, sizeof(buf2), + NULL, NULL); + if (strcmp(buf, buf2) != 0) { + wbuf[0] = L'\0'; + } +} + +static int mg_stat(const char *path, file_stat_t *st) { + wchar_t wpath[MAX_PATH_SIZE]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + DBG(("[%ls] -> %d", wpath, _wstati64(wpath, st))); + return _wstati64(wpath, st); +} + +static FILE *mg_fopen(const char *path, const char *mode) { + wchar_t wpath[MAX_PATH_SIZE], wmode[10]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + to_wchar(mode, wmode, ARRAY_SIZE(wmode)); + return _wfopen(wpath, wmode); +} + +static int mg_open(const char *path, int flag) { + wchar_t wpath[MAX_PATH_SIZE]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + return _wopen(wpath, flag); +} +#endif + +static void set_close_on_exec(int fd) { +#ifdef _WIN32 + (void) SetHandleInformation((HANDLE) fd, HANDLE_FLAG_INHERIT, 0); +#else + fcntl(fd, F_SETFD, FD_CLOEXEC); +#endif +} + +static void set_non_blocking_mode(sock_t sock) { +#ifdef _WIN32 + unsigned long on = 1; + ioctlsocket(sock, FIONBIO, &on); +#else + int flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags | O_NONBLOCK); +#endif +} + +// A helper function for traversing a comma separated list of values. +// It returns a list pointer shifted to the next value, or NULL if the end +// of the list found. +// Value is stored in val vector. If value has form "x=y", then eq_val +// vector is initialized to point to the "y" part, and val vector length +// is adjusted to point only to "x". +static const char *next_option(const char *list, struct vec *val, + struct vec *eq_val) { + if (list == NULL || *list == '\0') { + // End of the list + list = NULL; + } else { + val->ptr = list; + if ((list = strchr(val->ptr, ',')) != NULL) { + // Comma found. Store length and shift the list ptr + val->len = list - val->ptr; + list++; + } else { + // This value is the last one + list = val->ptr + strlen(val->ptr); + val->len = list - val->ptr; + } + + if (eq_val != NULL) { + // Value has form "x=y", adjust pointers and lengths + // so that val points to "x", and eq_val points to "y". + eq_val->len = 0; + eq_val->ptr = (const char *) memchr(val->ptr, '=', val->len); + if (eq_val->ptr != NULL) { + eq_val->ptr++; // Skip over '=' character + eq_val->len = val->ptr + val->len - eq_val->ptr; + val->len = (eq_val->ptr - val->ptr) - 1; + } + } + } + + return list; +} + +static int spool(struct iobuf *io, const void *buf, int len) { + static const double mult = 1.2; + char *p = NULL; + int new_len = 0; + + assert(io->len >= 0); + assert(io->len <= io->size); + + //DBG(("1. %d %d %d", len, io->len, io->size)); + if (len <= 0) { + } else if ((new_len = io->len + len) < io->size) { + memcpy(io->buf + io->len, buf, len); + io->len = new_len; + } else if ((p = (char *) realloc(io->buf, (int) (new_len * mult))) != NULL) { + io->buf = p; + memcpy(io->buf + io->len, buf, len); + io->len = new_len; + io->size = (int) (new_len * mult); + } else { + len = 0; + } + //DBG(("%d %d %d", len, io->len, io->size)); + + return len; +} + +// Like snprintf(), but never returns negative value, or a value +// that is larger than a supplied buffer. +static int mg_vsnprintf(char *buf, size_t buflen, const char *fmt, va_list ap) { + int n; + if (buflen < 1) return 0; + n = vsnprintf(buf, buflen, fmt, ap); + if (n < 0) { + n = 0; + } else if (n >= (int) buflen) { + n = (int) buflen - 1; + } + buf[n] = '\0'; + return n; +} + +static int mg_snprintf(char *buf, size_t buflen, const char *fmt, ...) { + va_list ap; + int n; + va_start(ap, fmt); + n = mg_vsnprintf(buf, buflen, fmt, ap); + va_end(ap); + return n; +} + +// Check whether full request is buffered. Return: +// -1 if request is malformed +// 0 if request is not yet fully buffered +// >0 actual request length, including last \r\n\r\n +static int get_request_len(const char *s, int buf_len) { + const unsigned char *buf = (unsigned char *) s; + int i; + + for (i = 0; i < buf_len; i++) { + // Control characters are not allowed but >=128 are. + // Abort scan as soon as one malformed character is found. + if (!isprint(buf[i]) && buf[i] != '\r' && buf[i] != '\n' && buf[i] < 128) { + return -1; + } else if (buf[i] == '\n' && i + 1 < buf_len && buf[i + 1] == '\n') { + return i + 2; + } else if (buf[i] == '\n' && i + 2 < buf_len && buf[i + 1] == '\r' && + buf[i + 2] == '\n') { + return i + 3; + } + } + + return 0; +} + +// Skip the characters until one of the delimiters characters found. +// 0-terminate resulting word. Skip the rest of the delimiters if any. +// Advance pointer to buffer to the next word. Return found 0-terminated word. +static char *skip(char **buf, const char *delimiters) { + char *p, *begin_word, *end_word, *end_delimiters; + + begin_word = *buf; + end_word = begin_word + strcspn(begin_word, delimiters); + end_delimiters = end_word + strspn(end_word, delimiters); + + for (p = end_word; p < end_delimiters; p++) { + *p = '\0'; + } + + *buf = end_delimiters; + + return begin_word; +} + +// Parse HTTP headers from the given buffer, advance buffer to the point +// where parsing stopped. +static void parse_http_headers(char **buf, struct mg_connection *ri) { + size_t i; + + for (i = 0; i < ARRAY_SIZE(ri->http_headers); i++) { + ri->http_headers[i].name = skip(buf, ": "); + ri->http_headers[i].value = skip(buf, "\r\n"); + if (ri->http_headers[i].name[0] == '\0') + break; + ri->num_headers = i + 1; + } +} + +static const char *status_code_to_str(int status_code) { + switch (status_code) { + case 200: return "OK"; + case 201: return "Created"; + case 204: return "No Content"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 304: return "Not Modified"; + case 400: return "Bad Request"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 409: return "Conflict"; + case 411: return "Length Required"; + case 413: return "Request Entity Too Large"; + case 415: return "Unsupported Media Type"; + case 423: return "Locked"; + case 500: return "Server Error"; + case 501: return "Not Implemented"; + default: return "Server Error"; + } +} + +static void send_http_error(struct connection *conn, int code, + const char *fmt, ...) { + const char *message = status_code_to_str(code); + const char *rewrites = conn->server->config_options[URL_REWRITES]; + char headers[200], body[200]; + struct vec a, b; + va_list ap; + int body_len, headers_len, match_code; + + conn->mg_conn.status_code = code; + + // Invoke error handler if it is set + if (conn->server->error_handler != NULL && + conn->server->error_handler(&conn->mg_conn) == MG_ERROR_PROCESSED) { + close_local_endpoint(conn); + return; + } + + // Handle error code rewrites + while ((rewrites = next_option(rewrites, &a, &b)) != NULL) { + if ((match_code = atoi(a.ptr)) > 0 && match_code == code) { + conn->mg_conn.status_code = 302; + mg_printf(&conn->mg_conn, "HTTP/1.1 %d Moved\r\n" + "Location: %.*s?code=%d&orig_uri=%s\r\n\r\n", + conn->mg_conn.status_code, b.len, b.ptr, code, + conn->mg_conn.uri); + close_local_endpoint(conn); + return; + } + } + + body_len = mg_snprintf(body, sizeof(body), "%d %s\n", code, message); + if (fmt != NULL) { + va_start(ap, fmt); + body_len += mg_vsnprintf(body + body_len, sizeof(body) - body_len, fmt, ap); + va_end(ap); + } + if (code >= 300 && code <= 399) { + // 3xx errors do not have body + body_len = 0; + } + headers_len = mg_snprintf(headers, sizeof(headers), + "HTTP/1.1 %d %s\r\nContent-Length: %d\r\n" + "Content-Type: text/plain\r\n\r\n", + code, message, body_len); + spool(&conn->remote_iobuf, headers, headers_len); + spool(&conn->remote_iobuf, body, body_len); + close_local_endpoint(conn); // This will write to the log file +} + +// Print message to buffer. If buffer is large enough to hold the message, +// return buffer. If buffer is to small, allocate large enough buffer on heap, +// and return allocated buffer. +static int alloc_vprintf(char **buf, size_t size, const char *fmt, va_list ap) { + va_list ap_copy; + int len; + + // Windows is not standard-compliant, and vsnprintf() returns -1 if + // buffer is too small. Also, older versions of msvcrt.dll do not have + // _vscprintf(). However, if size is 0, vsnprintf() behaves correctly. + // Therefore, we make two passes: on first pass, get required message length. + // On second pass, actually print the message. + va_copy(ap_copy, ap); + len = vsnprintf(NULL, 0, fmt, ap_copy); + + if (len > (int) size && + (size = len + 1) > 0 && + (*buf = (char *) malloc(size)) == NULL) { + len = -1; // Allocation failed, mark failure + } else { + va_copy(ap_copy, ap); + vsnprintf(*buf, size, fmt, ap_copy); + } + + return len; +} + +static void write_chunk(struct connection *conn, const char *buf, int len) { + char chunk_size[50]; + int n = mg_snprintf(chunk_size, sizeof(chunk_size), "%X\r\n", len); + spool(&conn->remote_iobuf, chunk_size, n); + spool(&conn->remote_iobuf, buf, len); + spool(&conn->remote_iobuf, "\r\n", 2); +} + +int mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap, + int chunked) { + char mem[IOBUF_SIZE], *buf = mem; + int len; + + if ((len = alloc_vprintf(&buf, sizeof(mem), fmt, ap)) > 0) { + if (chunked) { + write_chunk((struct connection *) conn, buf, len); + } else { + len = mg_write(conn, buf, (size_t) len); + } + } + if (buf != mem && buf != NULL) { + free(buf); + } + + return len; +} + +int mg_printf(struct mg_connection *conn, const char *fmt, ...) { + int len; + va_list ap; + va_start(ap, fmt); + len = mg_vprintf(conn, fmt, ap, 0); + va_end(ap); + return len; +} + +static int mg_socketpair(sock_t sp[2]) { + struct sockaddr_in sa; + sock_t sock, ret = -1; + socklen_t len = sizeof(sa); + + sp[0] = sp[1] = INVALID_SOCKET; + + (void) memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_port = htons(0); + sa.sin_addr.s_addr = htonl(0x7f000001); + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET && + !bind(sock, (struct sockaddr *) &sa, len) && + !listen(sock, 1) && + !getsockname(sock, (struct sockaddr *) &sa, &len) && + (sp[0] = socket(AF_INET, SOCK_STREAM, 6)) != -1 && + !connect(sp[0], (struct sockaddr *) &sa, len) && + (sp[1] = accept(sock,(struct sockaddr *) &sa, &len)) != INVALID_SOCKET) { + set_close_on_exec(sp[0]); + set_close_on_exec(sp[1]); + ret = 0; + } else { + if (sp[0] != INVALID_SOCKET) closesocket(sp[0]); + if (sp[1] != INVALID_SOCKET) closesocket(sp[1]); + sp[0] = sp[1] = INVALID_SOCKET; + } + closesocket(sock); + + return ret; +} + +static int is_error(int n) { + return n == 0 || + (n < 0 && errno != EINTR && errno != EINPROGRESS && + errno != EAGAIN && errno != EWOULDBLOCK +#ifdef _WIN32 + && WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK +#endif + ); +} + +static void discard_leading_iobuf_bytes(struct iobuf *io, int n) { + if (n >= 0 && n <= io->len) { + memmove(io->buf, io->buf + n, io->len - n); + io->len -= n; + } +} + +#ifndef MONGOOSE_NO_CGI +#ifdef _WIN32 +struct threadparam { + sock_t s; + HANDLE hPipe; +}; + +static int wait_until_ready(sock_t sock, int for_read) { + fd_set set; + FD_ZERO(&set); + FD_SET(sock, &set); + select(sock + 1, for_read ? &set : 0, for_read ? 0 : &set, 0, 0); + return 1; +} + +static void *push_to_stdin(void *arg) { + struct threadparam *tp = arg; + int n, sent, stop = 0; + DWORD k; + char buf[IOBUF_SIZE]; + + while (!stop && wait_until_ready(tp->s, 1) && + (n = recv(tp->s, buf, sizeof(buf), 0)) > 0) { + if (n == -1 && GetLastError() == WSAEWOULDBLOCK) continue; + for (sent = 0; !stop && sent < n; sent += k) { + if (!WriteFile(tp->hPipe, buf + sent, n - sent, &k, 0)) stop = 1; + } + } + DBG(("%s", "FORWARED EVERYTHING TO CGI")); + CloseHandle(tp->hPipe); + free(tp); + _endthread(); + return NULL; +} + +static void *pull_from_stdout(void *arg) { + struct threadparam *tp = arg; + int k, stop = 0; + DWORD n, sent; + char buf[IOBUF_SIZE]; + + while (!stop && ReadFile(tp->hPipe, buf, sizeof(buf), &n, NULL)) { + for (sent = 0; !stop && sent < n; sent += k) { + if (wait_until_ready(tp->s, 0) && + (k = send(tp->s, buf + sent, n - sent, 0)) <= 0) stop = 1; + } + } + DBG(("%s", "EOF FROM CGI")); + CloseHandle(tp->hPipe); + shutdown(tp->s, 2); // Without this, IO thread may get truncated data + closesocket(tp->s); + free(tp); + _endthread(); + return NULL; +} + +static void spawn_stdio_thread(sock_t sock, HANDLE hPipe, + void *(*func)(void *)) { + struct threadparam *tp = malloc(sizeof(*tp)); + if (tp != NULL) { + tp->s = sock; + tp->hPipe = hPipe; + mg_start_thread(func, tp); + } +} + +static void abs_path(const char *utf8_path, char *abs_path, size_t len) { + wchar_t buf[MAX_PATH_SIZE], buf2[MAX_PATH_SIZE]; + to_wchar(utf8_path, buf, ARRAY_SIZE(buf)); + GetFullPathNameW(buf, ARRAY_SIZE(buf2), buf2, NULL); + WideCharToMultiByte(CP_UTF8, 0, buf2, wcslen(buf2) + 1, abs_path, len, 0, 0); +} + +static pid_t start_process(char *interp, const char *cmd, const char *env, + const char *envp[], const char *dir, sock_t sock) { + STARTUPINFOW si = {0}; + PROCESS_INFORMATION pi = {0}; + HANDLE a[2], b[2], me = GetCurrentProcess(); + wchar_t wcmd[MAX_PATH_SIZE], full_dir[MAX_PATH_SIZE]; + char buf[MAX_PATH_SIZE], buf4[MAX_PATH_SIZE], buf5[MAX_PATH_SIZE], + cmdline[MAX_PATH_SIZE], *p; + DWORD flags = DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS; + FILE *fp; + + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + si.wShowWindow = SW_HIDE; + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + + CreatePipe(&a[0], &a[1], NULL, 0); + CreatePipe(&b[0], &b[1], NULL, 0); + DuplicateHandle(me, a[0], me, &si.hStdInput, 0, TRUE, flags); + DuplicateHandle(me, b[1], me, &si.hStdOutput, 0, TRUE, flags); + + if (interp == NULL && (fp = fopen(cmd, "r")) != NULL) { + buf[0] = buf[1] = '\0'; + fgets(buf, sizeof(buf), fp); + buf[sizeof(buf) - 1] = '\0'; + if (buf[0] == '#' && buf[1] == '!') { + interp = buf + 2; + for (p = interp + strlen(interp); + isspace(* (uint8_t *) p) && p > interp; p--) *p = '\0'; + } + fclose(fp); + } + + if (interp != NULL) { + abs_path(interp, buf4, ARRAY_SIZE(buf4)); + interp = buf4; + } + abs_path(dir, buf5, ARRAY_SIZE(buf5)); + to_wchar(dir, full_dir, ARRAY_SIZE(full_dir)); + mg_snprintf(cmdline, sizeof(cmdline), "%s%s\"%s\"", + interp ? interp : "", interp ? " " : "", cmd); + to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd)); + + if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, + (void *) env, full_dir, &si, &pi) != 0) { + spawn_stdio_thread(sock, a[1], push_to_stdin); + spawn_stdio_thread(sock, b[0], pull_from_stdout); + } else { + CloseHandle(a[1]); + CloseHandle(b[0]); + closesocket(sock); + } + DBG(("CGI command: [%ls] -> %p", wcmd, pi.hProcess)); + + CloseHandle(si.hStdOutput); + CloseHandle(si.hStdInput); + CloseHandle(a[0]); + CloseHandle(b[1]); + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + + return pi.hProcess; +} +#else +static pid_t start_process(const char *interp, const char *cmd, const char *env, + const char *envp[], const char *dir, sock_t sock) { + char buf[500]; + pid_t pid = fork(); + (void) env; + + if (pid == 0) { + (void) chdir(dir); + (void) dup2(sock, 0); + (void) dup2(sock, 1); + closesocket(sock); + + // After exec, all signal handlers are restored to their default values, + // with one exception of SIGCHLD. According to POSIX.1-2001 and Linux's + // implementation, SIGCHLD's handler will leave unchanged after exec + // if it was set to be ignored. Restore it to default action. + signal(SIGCHLD, SIG_DFL); + + if (interp == NULL) { + execle(cmd, cmd, NULL, envp); + } else { + execle(interp, interp, cmd, NULL, envp); + } + snprintf(buf, sizeof(buf), "Status: 500\r\n\r\n" + "500 Server Error: %s%s%s: %s", interp == NULL ? "" : interp, + interp == NULL ? "" : " ", cmd, strerror(errno)); + send(1, buf, strlen(buf), 0); + exit(EXIT_FAILURE); // exec call failed + } + + return pid; +} +#endif // _WIN32 + +// This structure helps to create an environment for the spawned CGI program. +// Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings, +// last element must be NULL. +// However, on Windows there is a requirement that all these VARIABLE=VALUE\0 +// strings must reside in a contiguous buffer. The end of the buffer is +// marked by two '\0' characters. +// We satisfy both worlds: we create an envp array (which is vars), all +// entries are actually pointers inside buf. +struct cgi_env_block { + struct mg_connection *conn; + char buf[CGI_ENVIRONMENT_SIZE]; // Environment buffer + const char *vars[MAX_CGI_ENVIR_VARS]; // char *envp[] + int len; // Space taken + int nvars; // Number of variables in envp[] +}; + +// Append VARIABLE=VALUE\0 string to the buffer, and add a respective +// pointer into the vars array. +static char *addenv(struct cgi_env_block *block, const char *fmt, ...) { + int n, space; + char *added; + va_list ap; + + // Calculate how much space is left in the buffer + space = sizeof(block->buf) - block->len - 2; + assert(space >= 0); + + // Make a pointer to the free space int the buffer + added = block->buf + block->len; + + // Copy VARIABLE=VALUE\0 string into the free space + va_start(ap, fmt); + n = mg_vsnprintf(added, (size_t) space, fmt, ap); + va_end(ap); + + // Make sure we do not overflow buffer and the envp array + if (n > 0 && n + 1 < space && + block->nvars < (int) ARRAY_SIZE(block->vars) - 2) { + // Append a pointer to the added string into the envp array + block->vars[block->nvars++] = added; + // Bump up used length counter. Include \0 terminator + block->len += n + 1; + } + + return added; +} + +static void addenv2(struct cgi_env_block *blk, const char *name) { + const char *s; + if ((s = getenv(name)) != NULL) addenv(blk, "%s=%s", name, s); +} + +static void prepare_cgi_environment(struct connection *conn, + const char *prog, + struct cgi_env_block *blk) { + struct mg_connection *ri = &conn->mg_conn; + const char *s, *slash; + char *p, **opts = conn->server->config_options; + int i; + + blk->len = blk->nvars = 0; + blk->conn = ri; + + addenv(blk, "SERVER_NAME=%s", opts[AUTH_DOMAIN]); + addenv(blk, "SERVER_ROOT=%s", opts[DOCUMENT_ROOT]); + addenv(blk, "DOCUMENT_ROOT=%s", opts[DOCUMENT_ROOT]); + addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MONGOOSE_VERSION); + + // Prepare the environment block + addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1"); + addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1"); + addenv(blk, "%s", "REDIRECT_STATUS=200"); // For PHP + + // TODO(lsm): fix this for IPv6 case + //addenv(blk, "SERVER_PORT=%d", ri->remote_port); + + addenv(blk, "REQUEST_METHOD=%s", ri->request_method); + addenv(blk, "REMOTE_ADDR=%s", ri->remote_ip); + addenv(blk, "REMOTE_PORT=%d", ri->remote_port); + addenv(blk, "REQUEST_URI=%s%s%s", ri->uri, + ri->query_string == NULL ? "" : "?", + ri->query_string == NULL ? "" : ri->query_string); + + // SCRIPT_NAME + if (conn->path_info != NULL) { + addenv(blk, "SCRIPT_NAME=%.*s", + (int) (strlen(ri->uri) - strlen(conn->path_info)), ri->uri); + addenv(blk, "PATH_INFO=%s", conn->path_info); + } else { + s = strrchr(prog, '/'); + slash = strrchr(ri->uri, '/'); + addenv(blk, "SCRIPT_NAME=%.*s%s", + slash == NULL ? 0 : (int) (slash - ri->uri), ri->uri, + s == NULL ? prog : s); + } + + addenv(blk, "SCRIPT_FILENAME=%s", prog); + addenv(blk, "PATH_TRANSLATED=%s", prog); +#ifdef MONGOOSE_USE_SSL + addenv(blk, "HTTPS=%s", conn->ssl != NULL ? "on" : "off"); +#else + addenv(blk, "HTTPS=%s", "off"); +#endif + + if ((s = mg_get_header(ri, "Content-Type")) != NULL) + addenv(blk, "CONTENT_TYPE=%s", s); + + if (ri->query_string != NULL) + addenv(blk, "QUERY_STRING=%s", ri->query_string); + + if ((s = mg_get_header(ri, "Content-Length")) != NULL) + addenv(blk, "CONTENT_LENGTH=%s", s); + + addenv2(blk, "PATH"); + addenv2(blk, "PERLLIB"); + addenv2(blk, ENV_EXPORT_TO_CGI); + +#if defined(_WIN32) + addenv2(blk, "COMSPEC"); + addenv2(blk, "SYSTEMROOT"); + addenv2(blk, "SystemDrive"); + addenv2(blk, "ProgramFiles"); + addenv2(blk, "ProgramFiles(x86)"); + addenv2(blk, "CommonProgramFiles(x86)"); +#else + addenv2(blk, "LD_LIBRARY_PATH"); +#endif // _WIN32 + + // Add all headers as HTTP_* variables + for (i = 0; i < ri->num_headers; i++) { + p = addenv(blk, "HTTP_%s=%s", + ri->http_headers[i].name, ri->http_headers[i].value); + + // Convert variable name into uppercase, and change - to _ + for (; *p != '=' && *p != '\0'; p++) { + if (*p == '-') + *p = '_'; + *p = (char) toupper(* (unsigned char *) p); + } + } + + blk->vars[blk->nvars++] = NULL; + blk->buf[blk->len++] = '\0'; + + assert(blk->nvars < (int) ARRAY_SIZE(blk->vars)); + assert(blk->len > 0); + assert(blk->len < (int) sizeof(blk->buf)); +} + +static const char cgi_status[] = "HTTP/1.1 200 OK\r\n"; + +static void open_cgi_endpoint(struct connection *conn, const char *prog) { + struct cgi_env_block blk; + char dir[MAX_PATH_SIZE], *p; + sock_t fds[2]; + + prepare_cgi_environment(conn, prog, &blk); + // CGI must be executed in its own directory. 'dir' must point to the + // directory containing executable program, 'p' must point to the + // executable program name relative to 'dir'. + if ((p = strrchr(prog, '/')) == NULL) { + mg_snprintf(dir, sizeof(dir), "%s", "."); + } else { + mg_snprintf(dir, sizeof(dir), "%.*s", (int) (p - prog), prog); + } + + // Try to create socketpair in a loop until success. mg_socketpair() + // can be interrupted by a signal and fail. + // TODO(lsm): use sigaction to restart interrupted syscall + do { + mg_socketpair(fds); + } while (fds[0] == INVALID_SOCKET); + + if (start_process(conn->server->config_options[CGI_INTERPRETER], + prog, blk.buf, blk.vars, dir, fds[1]) > 0) { + conn->endpoint_type = EP_CGI; + conn->endpoint.cgi_sock = fds[0]; + spool(&conn->remote_iobuf, cgi_status, sizeof(cgi_status) - 1); + conn->mg_conn.status_code = 200; + conn->flags |= CONN_BUFFER; + } else { + closesocket(fds[0]); + send_http_error(conn, 500, "start_process(%s) failed", prog); + } + +#ifndef _WIN32 + closesocket(fds[1]); // On Windows, CGI stdio thread closes that socket +#endif +} + +static void read_from_cgi(struct connection *conn) { + struct iobuf *io = &conn->remote_iobuf; + char buf[IOBUF_SIZE], buf2[sizeof(buf)], *s = buf2; + const char *status = "500"; + struct mg_connection c; + int len, s_len = sizeof(cgi_status) - 1, + n = recv(conn->endpoint.cgi_sock, buf, sizeof(buf), 0); + + DBG(("%p %d", conn, n)); + if (is_error(n)) { + close_local_endpoint(conn); + } else if (n > 0) { + spool(&conn->remote_iobuf, buf, n); + if (conn->flags & CONN_BUFFER) { + len = get_request_len(io->buf + s_len, io->len - s_len); + if (len == 0) return; + if (len > 0) { + memset(&c, 0, sizeof(c)); + memcpy(buf2, io->buf + s_len, len); + buf2[len - 1] = '\0'; + parse_http_headers(&s, &c); + if (mg_get_header(&c, "Location") != NULL) { + status = "302"; + } else if ((status = (char *) mg_get_header(&c, "Status")) == NULL) { + status = "200"; + } + } + memcpy(io->buf + 9, status, 3); + conn->mg_conn.status_code = atoi(status); + conn->flags &= ~CONN_BUFFER; + } + } +} + +static void forward_post_data(struct connection *conn) { + struct iobuf *io = &conn->local_iobuf; + int n = send(conn->endpoint.cgi_sock, io->buf, io->len, 0); + discard_leading_iobuf_bytes(io, n); +} +#endif // !MONGOOSE_NO_CGI + +// 'sa' must be an initialized address to bind to +static sock_t open_listening_socket(union socket_address *sa) { + socklen_t len = sizeof(*sa); + sock_t on = 1, sock = INVALID_SOCKET; + + if ((sock = socket(sa->sa.sa_family, SOCK_STREAM, 6)) != INVALID_SOCKET && + !setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on)) && + !bind(sock, &sa->sa, sa->sa.sa_family == AF_INET ? + sizeof(sa->sin) : sizeof(sa->sa)) && + !listen(sock, SOMAXCONN)) { + set_non_blocking_mode(sock); + // In case port was set to 0, get the real port number + (void) getsockname(sock, &sa->sa, &len); + } else if (sock != INVALID_SOCKET) { + closesocket(sock); + sock = INVALID_SOCKET; + } + + return sock; +} + +static char *mg_strdup(const char *str) { + char *copy = (char *) malloc(strlen(str) + 1); + if (copy != NULL) { + strcpy(copy, str); + } + return copy; +} + +static int isbyte(int n) { + return n >= 0 && n <= 255; +} + +static int parse_net(const char *spec, uint32_t *net, uint32_t *mask) { + int n, a, b, c, d, slash = 32, len = 0; + + if ((sscanf(spec, "%d.%d.%d.%d/%d%n", &a, &b, &c, &d, &slash, &n) == 5 || + sscanf(spec, "%d.%d.%d.%d%n", &a, &b, &c, &d, &n) == 4) && + isbyte(a) && isbyte(b) && isbyte(c) && isbyte(d) && + slash >= 0 && slash < 33) { + len = n; + *net = ((uint32_t)a << 24) | ((uint32_t)b << 16) | ((uint32_t)c << 8) | d; + *mask = slash ? 0xffffffffU << (32 - slash) : 0; + } + + return len; +} + +// Verify given socket address against the ACL. +// Return -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed. +static int check_acl(const char *acl, uint32_t remote_ip) { + int allowed, flag; + uint32_t net, mask; + struct vec vec; + + // If any ACL is set, deny by default + allowed = acl == NULL ? '+' : '-'; + + while ((acl = next_option(acl, &vec, NULL)) != NULL) { + flag = vec.ptr[0]; + if ((flag != '+' && flag != '-') || + parse_net(&vec.ptr[1], &net, &mask) == 0) { + return -1; + } + + if (net == (remote_ip & mask)) { + allowed = flag; + } + } + + return allowed == '+'; +} + +static void sockaddr_to_string(char *buf, size_t len, + const union socket_address *usa) { + buf[0] = '\0'; +#if defined(MONGOOSE_USE_IPV6) + inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ? + (void *) &usa->sin.sin_addr : + (void *) &usa->sin6.sin6_addr, buf, len); +#elif defined(_WIN32) + // Only Windoze Vista (and newer) have inet_ntop() + strncpy(buf, inet_ntoa(usa->sin.sin_addr), len); +#else + inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len); +#endif +} + +static struct connection *accept_new_connection(struct mg_server *server) { + union socket_address sa; + socklen_t len = sizeof(sa); + sock_t sock = INVALID_SOCKET; + struct connection *conn = NULL; + + // NOTE(lsm): on Windows, sock is always > FD_SETSIZE + if ((sock = accept(server->listening_sock, &sa.sa, &len)) == INVALID_SOCKET) { + } else if (!check_acl(server->config_options[ACCESS_CONTROL_LIST], + ntohl(* (uint32_t *) &sa.sin.sin_addr))) { + // NOTE(lsm): check_acl doesn't work for IPv6 + closesocket(sock); + } else if ((conn = (struct connection *) calloc(1, sizeof(*conn))) == NULL) { + closesocket(sock); +#ifdef MONGOOSE_USE_SSL + } else if (server->ssl_ctx != NULL && + ((conn->ssl = SSL_new(server->ssl_ctx)) == NULL || + SSL_set_fd(conn->ssl, sock) != 1)) { + DBG(("SSL error")); + closesocket(sock); + free(conn); + conn = NULL; +#endif + } else { + set_close_on_exec(sock); + set_non_blocking_mode(sock); + conn->server = server; + conn->client_sock = sock; + sockaddr_to_string(conn->mg_conn.remote_ip, + sizeof(conn->mg_conn.remote_ip), &sa); + conn->mg_conn.remote_port = ntohs(sa.sin.sin_port); + conn->mg_conn.server_param = server->server_data; + conn->mg_conn.local_ip = server->local_ip; + conn->mg_conn.local_port = ntohs(server->lsa.sin.sin_port); + LINKED_LIST_ADD_TO_FRONT(&server->active_connections, &conn->link); + DBG(("added conn %p", conn)); + } + + return conn; +} + +static void close_conn(struct connection *conn) { + LINKED_LIST_REMOVE(&conn->link); + closesocket(conn->client_sock); + close_local_endpoint(conn); + DBG(("%p %d %d", conn, conn->flags, conn->endpoint_type)); + free(conn->request); // It's OK to free(NULL), ditto below + free(conn->path_info); + free(conn->remote_iobuf.buf); + free(conn->local_iobuf.buf); +#ifdef MONGOOSE_USE_SSL + if (conn->ssl != NULL) SSL_free(conn->ssl); +#endif + free(conn); +} + +// Protect against directory disclosure attack by removing '..', +// excessive '/' and '\' characters +static void remove_double_dots_and_double_slashes(char *s) { + char *p = s; + + while (*s != '\0') { + *p++ = *s++; + if (s[-1] == '/' || s[-1] == '\\') { + // Skip all following slashes, backslashes and double-dots + while (s[0] != '\0') { + if (s[0] == '/' || s[0] == '\\') { s++; } + else if (s[0] == '.' && s[1] == '.') { s += 2; } + else { break; } + } + } + } + *p = '\0'; +} + +int mg_url_decode(const char *src, int src_len, char *dst, + int dst_len, int is_form_url_encoded) { + int i, j, a, b; +#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W') + + for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) { + if (src[i] == '%' && i < src_len - 2 && + isxdigit(* (const unsigned char *) (src + i + 1)) && + isxdigit(* (const unsigned char *) (src + i + 2))) { + a = tolower(* (const unsigned char *) (src + i + 1)); + b = tolower(* (const unsigned char *) (src + i + 2)); + dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b)); + i += 2; + } else if (is_form_url_encoded && src[i] == '+') { + dst[j] = ' '; + } else { + dst[j] = src[i]; + } + } + + dst[j] = '\0'; // Null-terminate the destination + + return i >= src_len ? j : -1; +} + +static int is_valid_http_method(const char *method) { + return !strcmp(method, "GET") || !strcmp(method, "POST") || + !strcmp(method, "HEAD") || !strcmp(method, "CONNECT") || + !strcmp(method, "PUT") || !strcmp(method, "DELETE") || + !strcmp(method, "OPTIONS") || !strcmp(method, "PROPFIND") + || !strcmp(method, "MKCOL"); +} + +// Parse HTTP request, fill in mg_request structure. +// This function modifies the buffer by NUL-terminating +// HTTP request components, header names and header values. +// Note that len must point to the last \n of HTTP headers. +static int parse_http_message(char *buf, int len, struct mg_connection *ri) { + int is_request, n; + + // Reset the connection. Make sure that we don't touch fields that are + // set elsewhere: remote_ip, remote_port, server_param + ri->request_method = ri->uri = ri->http_version = ri->query_string = NULL; + ri->num_headers = ri->status_code = ri->is_websocket = ri->content_len = 0; + + buf[len - 1] = '\0'; + + // RFC says that all initial whitespaces should be ingored + while (*buf != '\0' && isspace(* (unsigned char *) buf)) { + buf++; + } + ri->request_method = skip(&buf, " "); + ri->uri = skip(&buf, " "); + ri->http_version = skip(&buf, "\r\n"); + + // HTTP message could be either HTTP request or HTTP response, e.g. + // "GET / HTTP/1.0 ...." or "HTTP/1.0 200 OK ..." + is_request = is_valid_http_method(ri->request_method); + if ((is_request && memcmp(ri->http_version, "HTTP/", 5) != 0) || + (!is_request && memcmp(ri->request_method, "HTTP/", 5) != 0)) { + len = -1; + } else { + if (is_request) { + ri->http_version += 5; + } + parse_http_headers(&buf, ri); + + if ((ri->query_string = strchr(ri->uri, '?')) != NULL) { + *(char *) ri->query_string++ = '\0'; + } + n = (int) strlen(ri->uri); + mg_url_decode(ri->uri, n, (char *) ri->uri, n + 1, 0); + remove_double_dots_and_double_slashes((char *) ri->uri); + } + + return len; +} + +static int lowercase(const char *s) { + return tolower(* (const unsigned char *) s); +} + +static int mg_strcasecmp(const char *s1, const char *s2) { + int diff; + + do { + diff = lowercase(s1++) - lowercase(s2++); + } while (diff == 0 && s1[-1] != '\0'); + + return diff; +} + +static int mg_strncasecmp(const char *s1, const char *s2, size_t len) { + int diff = 0; + + if (len > 0) + do { + diff = lowercase(s1++) - lowercase(s2++); + } while (diff == 0 && s1[-1] != '\0' && --len > 0); + + return diff; +} + +// Return HTTP header value, or NULL if not found. +const char *mg_get_header(const struct mg_connection *ri, const char *s) { + int i; + + for (i = 0; i < ri->num_headers; i++) + if (!mg_strcasecmp(s, ri->http_headers[i].name)) + return ri->http_headers[i].value; + + return NULL; +} + +#ifndef MONGOOSE_NO_FILESYSTEM +// Perform case-insensitive match of string against pattern +static int match_prefix(const char *pattern, int pattern_len, const char *str) { + const char *or_str; + int len, res, i = 0, j = 0; + + if ((or_str = (const char *) memchr(pattern, '|', pattern_len)) != NULL) { + res = match_prefix(pattern, or_str - pattern, str); + return res > 0 ? res : + match_prefix(or_str + 1, (pattern + pattern_len) - (or_str + 1), str); + } + + for (; i < pattern_len; i++, j++) { + if (pattern[i] == '?' && str[j] != '\0') { + continue; + } else if (pattern[i] == '$') { + return str[j] == '\0' ? j : -1; + } else if (pattern[i] == '*') { + i++; + if (pattern[i] == '*') { + i++; + len = (int) strlen(str + j); + } else { + len = (int) strcspn(str + j, "/"); + } + if (i == pattern_len) { + return j + len; + } + do { + res = match_prefix(pattern + i, pattern_len - i, str + j + len); + } while (res == -1 && len-- > 0); + return res == -1 ? -1 : j + res + len; + } else if (lowercase(&pattern[i]) != lowercase(&str[j])) { + return -1; + } + } + return j; +} + +static int must_hide_file(struct connection *conn, const char *path) { + const char *pw_pattern = "**" PASSWORDS_FILE_NAME "$"; + const char *pattern = conn->server->config_options[HIDE_FILES_PATTERN]; + return match_prefix(pw_pattern, strlen(pw_pattern), path) > 0 || + (pattern != NULL && match_prefix(pattern, strlen(pattern), path) > 0); +} + +// Return 1 if real file has been found, 0 otherwise +static int convert_uri_to_file_name(struct connection *conn, char *buf, + size_t buf_len, file_stat_t *st) { + struct vec a, b; + const char *rewrites = conn->server->config_options[URL_REWRITES]; + const char *root = conn->server->config_options[DOCUMENT_ROOT]; +#ifndef MONGOOSE_NO_CGI + const char *cgi_pat = conn->server->config_options[CGI_PATTERN]; + char *p; +#endif + const char *uri = conn->mg_conn.uri; + int match_len; + + // No filesystem access + if (root == NULL) return 0; + + // Handle URL rewrites + mg_snprintf(buf, buf_len, "%s%s", root, uri); + while ((rewrites = next_option(rewrites, &a, &b)) != NULL) { + if ((match_len = match_prefix(a.ptr, a.len, uri)) > 0) { + mg_snprintf(buf, buf_len, "%.*s%s", (int) b.len, b.ptr, uri + match_len); + break; + } + } + + if (stat(buf, st) == 0) return 1; + +#ifndef MONGOOSE_NO_CGI + // Support PATH_INFO for CGI scripts. + for (p = buf + strlen(root) + 2; *p != '\0'; p++) { + if (*p == '/') { + *p = '\0'; + if (match_prefix(cgi_pat, strlen(cgi_pat), buf) > 0 && !stat(buf, st)) { + DBG(("!!!! [%s]", buf)); + *p = '/'; + conn->path_info = mg_strdup(p); + *p = '\0'; + return 1; + } + *p = '/'; + } + } +#endif + + return 0; +} +#endif // MONGOOSE_NO_FILESYSTEM + +static int should_keep_alive(const struct mg_connection *conn) { + const char *method = conn->request_method; + const char *http_version = conn->http_version; + const char *header = mg_get_header(conn, "Connection"); + return method != NULL && (!strcmp(method, "GET") || + ((struct connection *) conn)->endpoint_type == EP_USER) && + ((header != NULL && !mg_strcasecmp(header, "keep-alive")) || + (header == NULL && http_version && !strcmp(http_version, "1.1"))); +} + +int mg_write(struct mg_connection *c, const void *buf, int len) { + return spool(&((struct connection *) c)->remote_iobuf, buf, len); +} + +void mg_send_status(struct mg_connection *c, int status) { + if (c->status_code == 0) { + c->status_code = status; + mg_printf(c, "HTTP/1.1 %d %s\r\n", status, status_code_to_str(status)); + } +} + +void mg_send_header(struct mg_connection *c, const char *name, const char *v) { + if (c->status_code == 0) { + c->status_code = 200; + mg_printf(c, "HTTP/1.1 %d %s\r\n", 200, status_code_to_str(200)); + } + mg_printf(c, "%s: %s\r\n", name, v); +} + +static void terminate_headers(struct mg_connection *c) { + struct connection *conn = (struct connection *) c; + if (!(conn->flags & CONN_HEADERS_SENT)) { + mg_send_header(c, "Transfer-Encoding", "chunked"); + mg_write(c, "\r\n", 2); + conn->flags |= CONN_HEADERS_SENT; + } +} + +void mg_send_data(struct mg_connection *c, const void *data, int data_len) { + terminate_headers(c); + write_chunk((struct connection *) c, (const char *) data, data_len); +} + +void mg_printf_data(struct mg_connection *c, const char *fmt, ...) { + va_list ap; + + terminate_headers(c); + + va_start(ap, fmt); + mg_vprintf(c, fmt, ap, 1); + va_end(ap); +} + +#if !defined(NO_WEBSOCKET) || !defined(MONGOOSE_NO_AUTH) +static int is_big_endian(void) { + static const int n = 1; + return ((char *) &n)[0] == 0; +} +#endif + +#ifndef MONGOOSE_NO_WEBSOCKET +// START OF SHA-1 code +// Copyright(c) By Steve Reid <steve@edmweb.com> +#define SHA1HANDSOFF +#if defined(__sun) +#include "solarisfixes.h" +#endif + +union char64long16 { unsigned char c[64]; uint32_t l[16]; }; + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +static uint32_t blk0(union char64long16 *block, int i) { + // Forrest: SHA expect BIG_ENDIAN, swap if LITTLE_ENDIAN + if (!is_big_endian()) { + block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | + (rol(block->l[i], 8) & 0x00FF00FF); + } + return block->l[i]; +} + +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ + ^block->l[(i+2)&15]^block->l[i&15],1)) +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(block, i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + +typedef struct { + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +} SHA1_CTX; + +static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) { + uint32_t a, b, c, d, e; + union char64long16 block[1]; + + memcpy(block, buffer, 64); + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + // Erase working structures. The order of operations is important, + // used to ensure that compiler doesn't optimize those out. + memset(block, 0, sizeof(block)); + a = b = c = d = e = 0; + (void) a; (void) b; (void) c; (void) d; (void) e; +} + +static void SHA1Init(SHA1_CTX* context) { + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + +static void SHA1Update(SHA1_CTX* context, const unsigned char* data, + uint32_t len) { + uint32_t i, j; + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1]++; + context->count[1] += (len>>29); + j = (j >> 3) & 63; + if ((j + len) > 63) { + memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) { + SHA1Transform(context->state, &data[i]); + } + j = 0; + } + else i = 0; + memcpy(&context->buffer[j], &data[i], len - i); +} + +static void SHA1Final(unsigned char digest[20], SHA1_CTX* context) { + unsigned i; + unsigned char finalcount[8], c; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); + } + c = 0200; + SHA1Update(context, &c, 1); + while ((context->count[0] & 504) != 448) { + c = 0000; + SHA1Update(context, &c, 1); + } + SHA1Update(context, finalcount, 8); + for (i = 0; i < 20; i++) { + digest[i] = (unsigned char) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } + memset(context, '\0', sizeof(*context)); + memset(&finalcount, '\0', sizeof(finalcount)); +} +// END OF SHA1 CODE + +static void base64_encode(const unsigned char *src, int src_len, char *dst) { + static const char *b64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + int i, j, a, b, c; + + for (i = j = 0; i < src_len; i += 3) { + a = src[i]; + b = i + 1 >= src_len ? 0 : src[i + 1]; + c = i + 2 >= src_len ? 0 : src[i + 2]; + + dst[j++] = b64[a >> 2]; + dst[j++] = b64[((a & 3) << 4) | (b >> 4)]; + if (i + 1 < src_len) { + dst[j++] = b64[(b & 15) << 2 | (c >> 6)]; + } + if (i + 2 < src_len) { + dst[j++] = b64[c & 63]; + } + } + while (j % 4 != 0) { + dst[j++] = '='; + } + dst[j++] = '\0'; +} + +static void send_websocket_handshake(struct mg_connection *conn, + const char *key) { + static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + char buf[500], sha[20], b64_sha[sizeof(sha) * 2]; + SHA1_CTX sha_ctx; + + mg_snprintf(buf, sizeof(buf), "%s%s", key, magic); + SHA1Init(&sha_ctx); + SHA1Update(&sha_ctx, (unsigned char *) buf, strlen(buf)); + SHA1Final((unsigned char *) sha, &sha_ctx); + base64_encode((unsigned char *) sha, sizeof(sha), b64_sha); + mg_snprintf(buf, sizeof(buf), "%s%s%s", + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: ", b64_sha, "\r\n\r\n"); + + mg_write(conn, buf, strlen(buf)); +} + +static int deliver_websocket_frame(struct connection *conn) { + // Having buf unsigned char * is important, as it is used below in arithmetic + unsigned char *buf = (unsigned char *) conn->local_iobuf.buf; + int i, len, buf_len = conn->local_iobuf.len, frame_len = 0, + mask_len = 0, header_len = 0, data_len = 0, buffered = 0; + + if (buf_len >= 2) { + len = buf[1] & 127; + mask_len = buf[1] & 128 ? 4 : 0; + if (len < 126 && buf_len >= mask_len) { + data_len = len; + header_len = 2 + mask_len; + } else if (len == 126 && buf_len >= 4 + mask_len) { + header_len = 4 + mask_len; + data_len = ((((int) buf[2]) << 8) + buf[3]); + } else if (buf_len >= 10 + mask_len) { + header_len = 10 + mask_len; + data_len = (int) (((uint64_t) htonl(* (uint32_t *) &buf[2])) << 32) + + htonl(* (uint32_t *) &buf[6]); + } + } + + frame_len = header_len + data_len; + buffered = frame_len > 0 && frame_len <= buf_len; + + if (buffered) { + conn->mg_conn.content_len = data_len; + conn->mg_conn.content = (char *) buf + header_len; + conn->mg_conn.wsbits = buf[0]; + + // Apply mask if necessary + if (mask_len > 0) { + for (i = 0; i < data_len; i++) { + buf[i + header_len] ^= (buf + header_len - mask_len)[i % 4]; + } + } + + // Call the handler and remove frame from the iobuf + if (conn->server->request_handler(&conn->mg_conn) == MG_CLIENT_CLOSE) { + conn->flags |= CONN_SPOOL_DONE; + } + discard_leading_iobuf_bytes(&conn->local_iobuf, frame_len); + } + + return buffered; +} + +int mg_websocket_write(struct mg_connection* conn, int opcode, + const char *data, size_t data_len) { + unsigned char *copy; + size_t copy_len = 0; + int retval = -1; + + if ((copy = (unsigned char *) malloc(data_len + 10)) == NULL) { + return -1; + } + + copy[0] = 0x80 + (opcode & 0x0f); + + // Frame format: http://tools.ietf.org/html/rfc6455#section-5.2 + if (data_len < 126) { + // Inline 7-bit length field + copy[1] = data_len; + memcpy(copy + 2, data, data_len); + copy_len = 2 + data_len; + } else if (data_len <= 0xFFFF) { + // 16-bit length field + copy[1] = 126; + * (uint16_t *) (copy + 2) = (uint16_t) htons((uint16_t) data_len); + memcpy(copy + 4, data, data_len); + copy_len = 4 + data_len; + } else { + // 64-bit length field + copy[1] = 127; + * (uint32_t *) (copy + 2) = (uint32_t) + htonl((uint32_t) ((uint64_t) data_len >> 32)); + * (uint32_t *) (copy + 6) = (uint32_t) htonl(data_len & 0xffffffff); + memcpy(copy + 10, data, data_len); + copy_len = 10 + data_len; + } + + if (copy_len > 0) { + retval = mg_write(conn, copy, copy_len); + } + free(copy); + + return retval; +} + +static void send_websocket_handshake_if_requested(struct mg_connection *conn) { + const char *ver = mg_get_header(conn, "Sec-WebSocket-Version"), + *key = mg_get_header(conn, "Sec-WebSocket-Key"); + if (ver != NULL && key != NULL) { + conn->is_websocket = 1; + send_websocket_handshake(conn, key); + } +} + +static void ping_idle_websocket_connection(struct connection *conn, time_t t) { + if (t - conn->last_activity_time > MONGOOSE_USE_WEBSOCKET_PING_INTERVAL) { + mg_websocket_write(&conn->mg_conn, 0x9, "", 0); + } +} +#else +#define ping_idle_websocket_connection(conn, t) +#endif // !MONGOOSE_NO_WEBSOCKET + +static void write_terminating_chunk(struct connection *conn) { + mg_write(&conn->mg_conn, "0\r\n\r\n", 5); +} + +static int call_request_handler(struct connection *conn) { + int result; + conn->mg_conn.content = conn->local_iobuf.buf; + switch ((result = conn->server->request_handler(&conn->mg_conn))) { + case MG_REQUEST_CALL_AGAIN: conn->flags |= CONN_LONG_RUNNING; break; + case MG_REQUEST_NOT_PROCESSED: break; + default: + if (conn->flags & CONN_HEADERS_SENT) { + write_terminating_chunk(conn); + } + close_local_endpoint(conn); + break; + } + return result; +} + +static void callback_http_client_on_connect(struct connection *conn) { + int ok = 1, ret; + socklen_t len = sizeof(ok); + + conn->flags &= ~CONN_CONNECTING; + ret = getsockopt(conn->client_sock, SOL_SOCKET, SO_ERROR, (char *) &ok, &len); +#ifdef MONGOOSE_USE_SSL + if (ret == 0 && ok == 0 && conn->ssl != NULL) { + int res = SSL_connect(conn->ssl), ssl_err = SSL_get_error(conn->ssl, res); + //DBG(("%p res %d %d", conn, res, ssl_err)); + if (res == 1) { + conn->flags = CONN_SSL_HANDS_SHAKEN; + } else if (res == 0 || ssl_err == 2 || ssl_err == 3) { + conn->flags |= CONN_CONNECTING; + return; // Call us again + } else { + ok = 1; + } + } +#endif + conn->mg_conn.status_code = + (ret == 0 && ok == 0) ? MG_CONNECT_SUCCESS : MG_CONNECT_FAILURE; + if (conn->handler(&conn->mg_conn) || ok != 0) { + conn->flags |= CONN_CLOSE; + } +} + +static void write_to_socket(struct connection *conn) { + struct iobuf *io = &conn->remote_iobuf; + int n = 0; + + if (conn->endpoint_type == EP_CLIENT && conn->flags & CONN_CONNECTING) { + callback_http_client_on_connect(conn); + return; + } + +#ifdef MONGOOSE_USE_SSL + if (conn->ssl != NULL) { + n = SSL_write(conn->ssl, io->buf, io->len); + } else +#endif + { n = send(conn->client_sock, io->buf, io->len, 0); } + + DBG(("%p Written %d of %d(%d): [%.*s ...]", + conn, n, io->len, io->size, io->len < 40 ? io->len : 40, io->buf)); + + if (is_error(n)) { + conn->flags |= CONN_CLOSE; + } else if (n > 0) { + discard_leading_iobuf_bytes(io, n); + conn->num_bytes_sent += n; + } + + if (io->len == 0 && conn->flags & CONN_SPOOL_DONE) { + conn->flags |= CONN_CLOSE; + } +} + +const char *mg_get_mime_type(const char *path, const char *default_mime_type) { + const char *ext; + size_t i, path_len; + + path_len = strlen(path); + + for (i = 0; static_builtin_mime_types[i].extension != NULL; i++) { + ext = path + (path_len - static_builtin_mime_types[i].ext_len); + if (path_len > static_builtin_mime_types[i].ext_len && + mg_strcasecmp(ext, static_builtin_mime_types[i].extension) == 0) { + return static_builtin_mime_types[i].mime_type; + } + } + + return default_mime_type; +} + +#ifndef MONGOOSE_NO_FILESYSTEM +// Convert month to the month number. Return -1 on error, or month number +static int get_month_index(const char *s) { + static const char *month_names[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + int i; + + for (i = 0; i < (int) ARRAY_SIZE(month_names); i++) + if (!strcmp(s, month_names[i])) + return i; + + return -1; +} + +static int num_leap_years(int year) { + return year / 4 - year / 100 + year / 400; +} + +// Parse UTC date-time string, and return the corresponding time_t value. +static time_t parse_date_string(const char *datetime) { + static const unsigned short days_before_month[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + char month_str[32]; + int second, minute, hour, day, month, year, leap_days, days; + time_t result = (time_t) 0; + + if (((sscanf(datetime, "%d/%3s/%d %d:%d:%d", + &day, month_str, &year, &hour, &minute, &second) == 6) || + (sscanf(datetime, "%d %3s %d %d:%d:%d", + &day, month_str, &year, &hour, &minute, &second) == 6) || + (sscanf(datetime, "%*3s, %d %3s %d %d:%d:%d", + &day, month_str, &year, &hour, &minute, &second) == 6) || + (sscanf(datetime, "%d-%3s-%d %d:%d:%d", + &day, month_str, &year, &hour, &minute, &second) == 6)) && + year > 1970 && + (month = get_month_index(month_str)) != -1) { + leap_days = num_leap_years(year) - num_leap_years(1970); + year -= 1970; + days = year * 365 + days_before_month[month] + (day - 1) + leap_days; + result = days * 24 * 3600 + hour * 3600 + minute * 60 + second; + } + + return result; +} + +// Look at the "path" extension and figure what mime type it has. +// Store mime type in the vector. +static void get_mime_type(const struct mg_server *server, const char *path, + struct vec *vec) { + struct vec ext_vec, mime_vec; + const char *list, *ext; + size_t path_len; + + path_len = strlen(path); + + // Scan user-defined mime types first, in case user wants to + // override default mime types. + list = server->config_options[EXTRA_MIME_TYPES]; + while ((list = next_option(list, &ext_vec, &mime_vec)) != NULL) { + // ext now points to the path suffix + ext = path + path_len - ext_vec.len; + if (mg_strncasecmp(ext, ext_vec.ptr, ext_vec.len) == 0) { + *vec = mime_vec; + return; + } + } + + vec->ptr = mg_get_mime_type(path, "text/plain"); + vec->len = strlen(vec->ptr); +} + +static const char *suggest_connection_header(const struct mg_connection *conn) { + return should_keep_alive(conn) ? "keep-alive" : "close"; +} + +static void construct_etag(char *buf, size_t buf_len, const file_stat_t *st) { + mg_snprintf(buf, buf_len, "\"%lx.%" INT64_FMT "\"", + (unsigned long) st->st_mtime, (int64_t) st->st_size); +} + +// Return True if we should reply 304 Not Modified. +static int is_not_modified(const struct connection *conn, + const file_stat_t *stp) { + char etag[64]; + const char *ims = mg_get_header(&conn->mg_conn, "If-Modified-Since"); + const char *inm = mg_get_header(&conn->mg_conn, "If-None-Match"); + construct_etag(etag, sizeof(etag), stp); + return (inm != NULL && !mg_strcasecmp(etag, inm)) || + (ims != NULL && stp->st_mtime <= parse_date_string(ims)); +} + +// For given directory path, substitute it to valid index file. +// Return 0 if index file has been found, -1 if not found. +// If the file is found, it's stats is returned in stp. +static int find_index_file(struct connection *conn, char *path, + size_t path_len, file_stat_t *stp) { + const char *list = conn->server->config_options[INDEX_FILES]; + file_stat_t st; + struct vec filename_vec; + size_t n = strlen(path), found = 0; + + // The 'path' given to us points to the directory. Remove all trailing + // directory separator characters from the end of the path, and + // then append single directory separator character. + while (n > 0 && path[n - 1] == '/') { + n--; + } + path[n] = '/'; + + // Traverse index files list. For each entry, append it to the given + // path and see if the file exists. If it exists, break the loop + while ((list = next_option(list, &filename_vec, NULL)) != NULL) { + + // Ignore too long entries that may overflow path buffer + if (filename_vec.len > (int) (path_len - (n + 2))) + continue; + + // Prepare full path to the index file + strncpy(path + n + 1, filename_vec.ptr, filename_vec.len); + path[n + 1 + filename_vec.len] = '\0'; + + //DBG(("[%s]", path)); + + // Does it exist? + if (!stat(path, &st)) { + // Yes it does, break the loop + *stp = st; + found = 1; + break; + } + } + + // If no index file exists, restore directory path + if (!found) { + path[n] = '\0'; + } + + return found; +} + +static int parse_range_header(const char *header, int64_t *a, int64_t *b) { + return sscanf(header, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b); +} + +static void gmt_time_string(char *buf, size_t buf_len, time_t *t) { + strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(t)); +} + +static void open_file_endpoint(struct connection *conn, const char *path, + file_stat_t *st) { + char date[64], lm[64], etag[64], range[64], headers[500]; + const char *msg = "OK", *hdr; + time_t curtime = time(NULL); + int64_t r1, r2; + struct vec mime_vec; + int n; + + conn->endpoint_type = EP_FILE; + set_close_on_exec(conn->endpoint.fd); + conn->mg_conn.status_code = 200; + + get_mime_type(conn->server, path, &mime_vec); + conn->cl = st->st_size; + range[0] = '\0'; + + // If Range: header specified, act accordingly + r1 = r2 = 0; + hdr = mg_get_header(&conn->mg_conn, "Range"); + if (hdr != NULL && (n = parse_range_header(hdr, &r1, &r2)) > 0 && + r1 >= 0 && r2 >= 0) { + conn->mg_conn.status_code = 206; + conn->cl = n == 2 ? (r2 > conn->cl ? conn->cl : r2) - r1 + 1: conn->cl - r1; + mg_snprintf(range, sizeof(range), "Content-Range: bytes " + "%" INT64_FMT "-%" INT64_FMT "/%" INT64_FMT "\r\n", + r1, r1 + conn->cl - 1, (int64_t) st->st_size); + msg = "Partial Content"; + lseek(conn->endpoint.fd, r1, SEEK_SET); + } + + // Prepare Etag, Date, Last-Modified headers. Must be in UTC, according to + // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3 + gmt_time_string(date, sizeof(date), &curtime); + gmt_time_string(lm, sizeof(lm), &st->st_mtime); + construct_etag(etag, sizeof(etag), st); + + n = mg_snprintf(headers, sizeof(headers), + "HTTP/1.1 %d %s\r\n" + "Date: %s\r\n" + "Last-Modified: %s\r\n" + "Etag: %s\r\n" + "Content-Type: %.*s\r\n" + "Content-Length: %" INT64_FMT "\r\n" + "Connection: %s\r\n" + "Accept-Ranges: bytes\r\n" + "%s%s\r\n", + conn->mg_conn.status_code, msg, date, lm, etag, + (int) mime_vec.len, mime_vec.ptr, conn->cl, + suggest_connection_header(&conn->mg_conn), + range, MONGOOSE_USE_EXTRA_HTTP_HEADERS); + spool(&conn->remote_iobuf, headers, n); + + if (!strcmp(conn->mg_conn.request_method, "HEAD")) { + conn->flags |= CONN_SPOOL_DONE; + close(conn->endpoint.fd); + conn->endpoint_type = EP_NONE; + } +} + +#endif // MONGOOSE_NO_FILESYSTEM + +static void call_request_handler_if_data_is_buffered(struct connection *conn) { + struct iobuf *loc = &conn->local_iobuf; + struct mg_connection *c = &conn->mg_conn; + +#ifndef MONGOOSE_NO_WEBSOCKET + if (conn->mg_conn.is_websocket) { + do { } while (deliver_websocket_frame(conn)); + } else +#endif + if ((size_t) loc->len >= c->content_len && + call_request_handler(conn) == MG_REQUEST_NOT_PROCESSED) { + open_local_endpoint(conn, 1); + } +} + +#if !defined(MONGOOSE_NO_DIRECTORY_LISTING) || !defined(MONGOOSE_NO_DAV) + +#ifdef _WIN32 +struct dirent { + char d_name[MAX_PATH_SIZE]; +}; + +typedef struct DIR { + HANDLE handle; + WIN32_FIND_DATAW info; + struct dirent result; +} DIR; + +// Implementation of POSIX opendir/closedir/readdir for Windows. +static DIR *opendir(const char *name) { + DIR *dir = NULL; + wchar_t wpath[MAX_PATH_SIZE]; + DWORD attrs; + + if (name == NULL) { + SetLastError(ERROR_BAD_ARGUMENTS); + } else if ((dir = (DIR *) malloc(sizeof(*dir))) == NULL) { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + } else { + to_wchar(name, wpath, ARRAY_SIZE(wpath)); + attrs = GetFileAttributesW(wpath); + if (attrs != 0xFFFFFFFF && + ((attrs & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)) { + (void) wcscat(wpath, L"\\*"); + dir->handle = FindFirstFileW(wpath, &dir->info); + dir->result.d_name[0] = '\0'; + } else { + free(dir); + dir = NULL; + } + } + + return dir; +} + +static int closedir(DIR *dir) { + int result = 0; + + if (dir != NULL) { + if (dir->handle != INVALID_HANDLE_VALUE) + result = FindClose(dir->handle) ? 0 : -1; + + free(dir); + } else { + result = -1; + SetLastError(ERROR_BAD_ARGUMENTS); + } + + return result; +} + +static struct dirent *readdir(DIR *dir) { + struct dirent *result = 0; + + if (dir) { + if (dir->handle != INVALID_HANDLE_VALUE) { + result = &dir->result; + (void) WideCharToMultiByte(CP_UTF8, 0, + dir->info.cFileName, -1, result->d_name, + sizeof(result->d_name), NULL, NULL); + + if (!FindNextFileW(dir->handle, &dir->info)) { + (void) FindClose(dir->handle); + dir->handle = INVALID_HANDLE_VALUE; + } + + } else { + SetLastError(ERROR_FILE_NOT_FOUND); + } + } else { + SetLastError(ERROR_BAD_ARGUMENTS); + } + + return result; +} +#endif // _WIN32 POSIX opendir/closedir/readdir implementation + +static int scan_directory(struct connection *conn, const char *dir, + struct dir_entry **arr) { + char path[MAX_PATH_SIZE]; + struct dir_entry *p; + struct dirent *dp; + int arr_size = 0, arr_ind = 0, inc = 100; + DIR *dirp; + + *arr = NULL; + if ((dirp = (opendir(dir))) == NULL) return 0; + + while ((dp = readdir(dirp)) != NULL) { + // Do not show current dir and hidden files + if (!strcmp(dp->d_name, ".") || + !strcmp(dp->d_name, "..") || + must_hide_file(conn, dp->d_name)) { + continue; + } + mg_snprintf(path, sizeof(path), "%s%c%s", dir, '/', dp->d_name); + + // Resize the array if nesessary + if (arr_ind >= arr_size) { + if ((p = (struct dir_entry *) + realloc(*arr, (inc + arr_size) * sizeof(**arr))) != NULL) { + // Memset new chunk to zero, otherwize st_mtime will have garbage which + // can make strftime() segfault, see + // http://code.google.com/p/mongoose/issues/detail?id=79 + memset(p + arr_size, 0, sizeof(**arr) * inc); + + *arr = p; + arr_size += inc; + } + } + + if (arr_ind < arr_size) { + (*arr)[arr_ind].conn = conn; + (*arr)[arr_ind].file_name = strdup(dp->d_name); + stat(path, &(*arr)[arr_ind].st); + arr_ind++; + } + } + closedir(dirp); + + return arr_ind; +} + +static void mg_url_encode(const char *src, char *dst, size_t dst_len) { + static const char *dont_escape = "._-$,;~()"; + static const char *hex = "0123456789abcdef"; + const char *end = dst + dst_len - 1; + + for (; *src != '\0' && dst < end; src++, dst++) { + if (isalnum(*(const unsigned char *) src) || + strchr(dont_escape, * (const unsigned char *) src) != NULL) { + *dst = *src; + } else if (dst + 2 < end) { + dst[0] = '%'; + dst[1] = hex[(* (const unsigned char *) src) >> 4]; + dst[2] = hex[(* (const unsigned char *) src) & 0xf]; + dst += 2; + } + } + + *dst = '\0'; +} +#endif // !NO_DIRECTORY_LISTING || !MONGOOSE_NO_DAV + +#ifndef MONGOOSE_NO_DIRECTORY_LISTING + +static void print_dir_entry(const struct dir_entry *de) { + char size[64], mod[64], href[MAX_PATH_SIZE * 3], chunk[MAX_PATH_SIZE * 4]; + int64_t fsize = de->st.st_size; + int is_dir = S_ISDIR(de->st.st_mode), n; + const char *slash = is_dir ? "/" : ""; + + if (is_dir) { + mg_snprintf(size, sizeof(size), "%s", "[DIRECTORY]"); + } else { + // We use (signed) cast below because MSVC 6 compiler cannot + // convert unsigned __int64 to double. + if (fsize < 1024) { + mg_snprintf(size, sizeof(size), "%d", (int) fsize); + } else if (fsize < 0x100000) { + mg_snprintf(size, sizeof(size), "%.1fk", (double) fsize / 1024.0); + } else if (fsize < 0x40000000) { + mg_snprintf(size, sizeof(size), "%.1fM", (double) fsize / 1048576); + } else { + mg_snprintf(size, sizeof(size), "%.1fG", (double) fsize / 1073741824); + } + } + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&de->st.st_mtime)); + mg_url_encode(de->file_name, href, sizeof(href)); + n = mg_snprintf(chunk, sizeof(chunk), + "<tr><td><a href=\"%s%s%s\">%s%s</a></td>" + "<td> %s</td><td> %s</td></tr>\n", + de->conn->mg_conn.uri, href, slash, de->file_name, slash, + mod, size); + write_chunk((struct connection *) de->conn, chunk, n); +} + +// Sort directory entries by size, or name, or modification time. +// On windows, __cdecl specification is needed in case if project is built +// with __stdcall convention. qsort always requires __cdels callback. +static int __cdecl compare_dir_entries(const void *p1, const void *p2) { + const struct dir_entry *a = (const struct dir_entry *) p1, + *b = (const struct dir_entry *) p2; + const char *qs = a->conn->mg_conn.query_string ? + a->conn->mg_conn.query_string : "na"; + int cmp_result = 0; + + if (S_ISDIR(a->st.st_mode) && !S_ISDIR(b->st.st_mode)) { + return -1; // Always put directories on top + } else if (!S_ISDIR(a->st.st_mode) && S_ISDIR(b->st.st_mode)) { + return 1; // Always put directories on top + } else if (*qs == 'n') { + cmp_result = strcmp(a->file_name, b->file_name); + } else if (*qs == 's') { + cmp_result = a->st.st_size == b->st.st_size ? 0 : + a->st.st_size > b->st.st_size ? 1 : -1; + } else if (*qs == 'd') { + cmp_result = a->st.st_mtime == b->st.st_mtime ? 0 : + a->st.st_mtime > b->st.st_mtime ? 1 : -1; + } + + return qs[1] == 'd' ? -cmp_result : cmp_result; +} + +static void send_directory_listing(struct connection *conn, const char *dir) { + char buf[2000]; + struct dir_entry *arr = NULL; + int i, num_entries, sort_direction = conn->mg_conn.query_string != NULL && + conn->mg_conn.query_string[1] == 'd' ? 'a' : 'd'; + + conn->mg_conn.status_code = 200; + mg_snprintf(buf, sizeof(buf), "%s", + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: Chunked\r\n" + "Content-Type: text/html; charset=utf-8\r\n\r\n"); + spool(&conn->remote_iobuf, buf, strlen(buf)); + + mg_snprintf(buf, sizeof(buf), + "<html><head><title>Index of %s</title>" + "<style>th {text-align: left;}</style></head>" + "<body><h1>Index of %s</h1><pre><table cellpadding=\"0\">" + "<tr><th><a href=\"?n%c\">Name</a></th>" + "<th><a href=\"?d%c\">Modified</a></th>" + "<th><a href=\"?s%c\">Size</a></th></tr>" + "<tr><td colspan=\"3\"><hr></td></tr>", + conn->mg_conn.uri, conn->mg_conn.uri, + sort_direction, sort_direction, sort_direction); + write_chunk(conn, buf, strlen(buf)); + + num_entries = scan_directory(conn, dir, &arr); + qsort(arr, num_entries, sizeof(arr[0]), compare_dir_entries); + for (i = 0; i < num_entries; i++) { + print_dir_entry(&arr[i]); + free(arr[i].file_name); + } + free(arr); + + write_terminating_chunk(conn); + close_local_endpoint(conn); +} +#endif // MONGOOSE_NO_DIRECTORY_LISTING + +#ifndef MONGOOSE_NO_DAV +static void print_props(struct connection *conn, const char *uri, + file_stat_t *stp) { + char mtime[64], buf[MAX_PATH_SIZE + 200]; + + gmt_time_string(mtime, sizeof(mtime), &stp->st_mtime); + mg_snprintf(buf, sizeof(buf), + "<d:response>" + "<d:href>%s</d:href>" + "<d:propstat>" + "<d:prop>" + "<d:resourcetype>%s</d:resourcetype>" + "<d:getcontentlength>%" INT64_FMT "</d:getcontentlength>" + "<d:getlastmodified>%s</d:getlastmodified>" + "</d:prop>" + "<d:status>HTTP/1.1 200 OK</d:status>" + "</d:propstat>" + "</d:response>\n", + uri, S_ISDIR(stp->st_mode) ? "<d:collection/>" : "", + (int64_t) stp->st_size, mtime); + spool(&conn->remote_iobuf, buf, strlen(buf)); +} + +static void handle_propfind(struct connection *conn, const char *path, + file_stat_t *stp) { + static const char header[] = "HTTP/1.1 207 Multi-Status\r\n" + "Connection: close\r\n" + "Content-Type: text/xml; charset=utf-8\r\n\r\n" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<d:multistatus xmlns:d='DAV:'>\n"; + static const char footer[] = "</d:multistatus>"; + const char *depth = mg_get_header(&conn->mg_conn, "Depth"), + *list_dir = conn->server->config_options[ENABLE_DIRECTORY_LISTING]; + + conn->mg_conn.status_code = 207; + spool(&conn->remote_iobuf, header, sizeof(header) - 1); + + // Print properties for the requested resource itself + print_props(conn, conn->mg_conn.uri, stp); + + // If it is a directory, print directory entries too if Depth is not 0 + if (S_ISDIR(stp->st_mode) && !mg_strcasecmp(list_dir, "yes") && + (depth == NULL || strcmp(depth, "0") != 0)) { + struct dir_entry *arr = NULL; + int i, num_entries = scan_directory(conn, path, &arr); + + for (i = 0; i < num_entries; i++) { + char buf[MAX_PATH_SIZE], buf2[sizeof(buf) * 3]; + struct dir_entry *de = &arr[i]; + + mg_snprintf(buf, sizeof(buf), "%s%s", de->conn->mg_conn.uri, + de->file_name); + mg_url_encode(buf, buf2, sizeof(buf2) - 1); + print_props(conn, buf, &de->st); + } + } + + spool(&conn->remote_iobuf, footer, sizeof(footer) - 1); + close_local_endpoint(conn); +} + +static void handle_mkcol(struct connection *conn, const char *path) { + int status_code = 500; + + if (conn->mg_conn.content_len > 0) { + status_code = 415; + } else if (!mkdir(path, 0755)) { + status_code = 201; + } else if (errno == EEXIST) { + status_code = 405; + } else if (errno == EACCES) { + status_code = 403; + } else if (errno == ENOENT) { + status_code = 409; + } + send_http_error(conn, status_code, NULL); +} + +static int remove_directory(const char *dir) { + char path[MAX_PATH_SIZE]; + struct dirent *dp; + file_stat_t st; + DIR *dirp; + + if ((dirp = opendir(dir)) == NULL) return 0; + + while ((dp = readdir(dirp)) != NULL) { + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; + mg_snprintf(path, sizeof(path), "%s%c%s", dir, '/', dp->d_name); + stat(path, &st); + if (S_ISDIR(st.st_mode)) { + remove_directory(path); + } else { + remove(path); + } + } + closedir(dirp); + rmdir(dir); + + return 1; +} + +static void handle_delete(struct connection *conn, const char *path) { + file_stat_t st; + + if (stat(path, &st) != 0) { + send_http_error(conn, 404, NULL); + } else if (S_ISDIR(st.st_mode)) { + remove_directory(path); + send_http_error(conn, 204, NULL); + } else if (!remove(path) == 0) { + send_http_error(conn, 204, NULL); + } else { + send_http_error(conn, 423, NULL); + } +} + +// For a given PUT path, create all intermediate subdirectories +// for given path. Return 0 if the path itself is a directory, +// or -1 on error, 1 if OK. +static int put_dir(const char *path) { + char buf[MAX_PATH_SIZE]; + const char *s, *p; + file_stat_t st; + + // Create intermediate directories if they do not exist + for (s = p = path + 1; (p = strchr(s, '/')) != NULL; s = ++p) { + if (p - path >= (int) sizeof(buf)) return -1; // Buffer overflow + memcpy(buf, path, p - path); + buf[p - path] = '\0'; + if (stat(buf, &st) != 0 && mkdir(buf, 0755) != 0) return -1; + if (p[1] == '\0') return 0; // Path is a directory itself + } + + return 1; +} + +static void handle_put(struct connection *conn, const char *path) { + file_stat_t st; + const char *range, *cl_hdr = mg_get_header(&conn->mg_conn, "Content-Length"); + int64_t r1, r2; + int rc; + + conn->mg_conn.status_code = !stat(path, &st) ? 200 : 201; + if ((rc = put_dir(path)) == 0) { + mg_printf(&conn->mg_conn, "HTTP/1.1 %d OK\r\n\r\n", + conn->mg_conn.status_code); + close_local_endpoint(conn); + } else if (rc == -1) { + send_http_error(conn, 500, "put_dir: %s", strerror(errno)); + } else if (cl_hdr == NULL) { + send_http_error(conn, 411, NULL); +#ifdef _WIN32 + //On Windows, open() is a macro with 2 params + } else if ((conn->endpoint.fd = + open(path, O_RDWR | O_CREAT | O_TRUNC)) < 0) { +#else + } else if ((conn->endpoint.fd = + open(path, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) { +#endif + send_http_error(conn, 500, "open(%s): %s", path, strerror(errno)); + } else { + DBG(("PUT [%s] %d", path, conn->local_iobuf.len)); + conn->endpoint_type = EP_PUT; + set_close_on_exec(conn->endpoint.fd); + range = mg_get_header(&conn->mg_conn, "Content-Range"); + conn->cl = to64(cl_hdr); + r1 = r2 = 0; + if (range != NULL && parse_range_header(range, &r1, &r2) > 0) { + conn->mg_conn.status_code = 206; + lseek(conn->endpoint.fd, r1, SEEK_SET); + conn->cl = r2 > r1 ? r2 - r1 + 1: conn->cl - r1; + } + mg_printf(&conn->mg_conn, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", + conn->mg_conn.status_code); + } +} + +static void forward_put_data(struct connection *conn) { + struct iobuf *io = &conn->local_iobuf; + int n = write(conn->endpoint.fd, io->buf, io->len); + if (n > 0) { + discard_leading_iobuf_bytes(io, n); + conn->cl -= n; + if (conn->cl <= 0) { + close_local_endpoint(conn); + } + } +} +#endif // MONGOOSE_NO_DAV + +static void send_options(struct connection *conn) { + static const char reply[] = "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, " + "CONNECT, PUT, DELETE, OPTIONS, PROPFIND, MKCOL\r\nDAV: 1\r\n\r\n"; + spool(&conn->remote_iobuf, reply, sizeof(reply) - 1); + conn->flags |= CONN_SPOOL_DONE; +} + +#ifndef MONGOOSE_NO_AUTH +void mg_send_digest_auth_request(struct mg_connection *c) { + struct connection *conn = (struct connection *) c; + c->status_code = 401; + mg_printf(c, + "HTTP/1.1 401 Unauthorized\r\n" + "WWW-Authenticate: Digest qop=\"auth\", " + "realm=\"%s\", nonce=\"%lu\"\r\n\r\n", + conn->server->config_options[AUTH_DOMAIN], + (unsigned long) time(NULL)); + close_local_endpoint(conn); +} + +// Use the global passwords file, if specified by auth_gpass option, +// or search for .htpasswd in the requested directory. +static FILE *open_auth_file(struct connection *conn, const char *path) { + char name[MAX_PATH_SIZE]; + const char *p, *gpass = conn->server->config_options[GLOBAL_AUTH_FILE]; + file_stat_t st; + FILE *fp = NULL; + + if (gpass != NULL) { + // Use global passwords file + fp = fopen(gpass, "r"); + } else if (!stat(path, &st) && S_ISDIR(st.st_mode)) { + mg_snprintf(name, sizeof(name), "%s%c%s", path, '/', PASSWORDS_FILE_NAME); + fp = fopen(name, "r"); + } else { + // Try to find .htpasswd in requested directory. + if ((p = strrchr(path, '/')) == NULL) p = path; + mg_snprintf(name, sizeof(name), "%.*s%c%s", + (int) (p - path), path, '/', PASSWORDS_FILE_NAME); + fp = fopen(name, "r"); + } + + return fp; +} + +#if !defined(HAVE_MD5) && !defined(MONGOOSE_NO_AUTH) +typedef struct MD5Context { + uint32_t buf[4]; + uint32_t bits[2]; + unsigned char in[64]; +} MD5_CTX; + +static void byteReverse(unsigned char *buf, unsigned longs) { + uint32_t t; + + // Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN + if (is_big_endian()) { + do { + t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + * (uint32_t *) buf = t; + buf += 4; + } while (--longs); + } +} + +#define F1(x, y, z) (z ^ (x & (y ^ z))) +#define F2(x, y, z) F1(z, x, y) +#define F3(x, y, z) (x ^ y ^ z) +#define F4(x, y, z) (y ^ (x | ~z)) + +#define MD5STEP(f, w, x, y, z, data, s) \ + ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) + +// Start MD5 accumulation. Set bit count to 0 and buffer to mysterious +// initialization constants. +static void MD5Init(MD5_CTX *ctx) { + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; + + ctx->bits[0] = 0; + ctx->bits[1] = 0; +} + +static void MD5Transform(uint32_t buf[4], uint32_t const in[16]) { + register uint32_t a, b, c, d; + + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; + + MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); + MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); + MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); + MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); + MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); + MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); + MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); + MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); + MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); + MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); + MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); + MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); + MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); + MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); + MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); + MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); + + MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); + MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); + MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); + MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); + MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); + MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); + MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); + MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); + MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); + MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); + MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); + MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); + MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); + MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); + MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); + MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); + + MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); + MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); + MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); + MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); + MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); + MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); + MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); + MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); + MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); + MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); + MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); + MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); + MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); + MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); + MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); + MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); + + MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); + MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); + MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); + MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); + MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); + MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); + MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); + MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); + MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); + MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); + MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); + MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); + MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); + MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); + MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); + MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); + + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; +} + +static void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len) { + uint32_t t; + + t = ctx->bits[0]; + if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t) + ctx->bits[1]++; + ctx->bits[1] += len >> 29; + + t = (t >> 3) & 0x3f; + + if (t) { + unsigned char *p = (unsigned char *) ctx->in + t; + + t = 64 - t; + if (len < t) { + memcpy(p, buf, len); + return; + } + memcpy(p, buf, t); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + buf += t; + len -= t; + } + + while (len >= 64) { + memcpy(ctx->in, buf, 64); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + buf += 64; + len -= 64; + } + + memcpy(ctx->in, buf, len); +} + +static void MD5Final(unsigned char digest[16], MD5_CTX *ctx) { + unsigned count; + unsigned char *p; + uint32_t *a; + + count = (ctx->bits[0] >> 3) & 0x3F; + + p = ctx->in + count; + *p++ = 0x80; + count = 64 - 1 - count; + if (count < 8) { + memset(p, 0, count); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + memset(ctx->in, 0, 56); + } else { + memset(p, 0, count - 8); + } + byteReverse(ctx->in, 14); + + a = (uint32_t *)ctx->in; + a[14] = ctx->bits[0]; + a[15] = ctx->bits[1]; + + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + byteReverse((unsigned char *) ctx->buf, 4); + memcpy(digest, ctx->buf, 16); + memset((char *) ctx, 0, sizeof(*ctx)); +} +#endif // !HAVE_MD5 + + + +// Stringify binary data. Output buffer must be twice as big as input, +// because each byte takes 2 bytes in string representation +static void bin2str(char *to, const unsigned char *p, size_t len) { + static const char *hex = "0123456789abcdef"; + + for (; len--; p++) { + *to++ = hex[p[0] >> 4]; + *to++ = hex[p[0] & 0x0f]; + } + *to = '\0'; +} + +// Return stringified MD5 hash for list of strings. Buffer must be 33 bytes. +char *mg_md5(char buf[33], ...) { + unsigned char hash[16]; + const char *p; + va_list ap; + MD5_CTX ctx; + + MD5Init(&ctx); + + va_start(ap, buf); + while ((p = va_arg(ap, const char *)) != NULL) { + MD5Update(&ctx, (const unsigned char *) p, (unsigned) strlen(p)); + } + va_end(ap); + + MD5Final(hash, &ctx); + bin2str(buf, hash, sizeof(hash)); + return buf; +} + +// Check the user's password, return 1 if OK +static int check_password(const char *method, const char *ha1, const char *uri, + const char *nonce, const char *nc, const char *cnonce, + const char *qop, const char *response) { + char ha2[32 + 1], expected_response[32 + 1]; + +#if 0 + // Check for authentication timeout + if ((unsigned long) time(NULL) - (unsigned long) to64(nonce) > 3600) { + return 0; + } +#endif + + mg_md5(ha2, method, ":", uri, NULL); + mg_md5(expected_response, ha1, ":", nonce, ":", nc, + ":", cnonce, ":", qop, ":", ha2, NULL); + + return mg_strcasecmp(response, expected_response) == 0 ? + MG_AUTH_OK : MG_AUTH_FAIL; +} + + +// Authorize against the opened passwords file. Return 1 if authorized. +int mg_authorize_digest(struct mg_connection *c, FILE *fp) { + struct connection *conn = (struct connection *) c; + const char *hdr; + char line[256], f_user[256], ha1[256], f_domain[256], user[100], nonce[100], + uri[MAX_REQUEST_SIZE], cnonce[100], resp[100], qop[100], nc[100]; + + if (c == NULL || fp == NULL) return 0; + if ((hdr = mg_get_header(c, "Authorization")) == NULL || + mg_strncasecmp(hdr, "Digest ", 7) != 0) return 0; + if (!mg_parse_header(hdr, "username", user, sizeof(user))) return 0; + if (!mg_parse_header(hdr, "cnonce", cnonce, sizeof(cnonce))) return 0; + if (!mg_parse_header(hdr, "response", resp, sizeof(resp))) return 0; + if (!mg_parse_header(hdr, "uri", uri, sizeof(uri))) return 0; + if (!mg_parse_header(hdr, "qop", qop, sizeof(qop))) return 0; + if (!mg_parse_header(hdr, "nc", nc, sizeof(nc))) return 0; + if (!mg_parse_header(hdr, "nonce", nonce, sizeof(nonce))) return 0; + + while (fgets(line, sizeof(line), fp) != NULL) { + if (sscanf(line, "%[^:]:%[^:]:%s", f_user, f_domain, ha1) == 3 && + !strcmp(user, f_user) && + // NOTE(lsm): due to a bug in MSIE, we do not compare URIs + !strcmp(conn->server->config_options[AUTH_DOMAIN], f_domain)) + return check_password(c->request_method, ha1, uri, + nonce, nc, cnonce, qop, resp); + } + return MG_AUTH_FAIL; +} + + +// Return 1 if request is authorised, 0 otherwise. +static int is_authorized(struct connection *conn, const char *path) { + FILE *fp; + int authorized = MG_AUTH_OK; + + if ((fp = open_auth_file(conn, path)) != NULL) { + authorized = mg_authorize_digest(&conn->mg_conn, fp); + fclose(fp); + } + + return authorized; +} + +static int is_authorized_for_dav(struct connection *conn) { + const char *auth_file = conn->server->config_options[DAV_AUTH_FILE]; + FILE *fp; + int authorized = MG_AUTH_FAIL; + + if (auth_file != NULL && (fp = fopen(auth_file, "r")) != NULL) { + authorized = mg_authorize_digest(&conn->mg_conn, fp); + fclose(fp); + } + + return authorized; +} + +static int is_dav_mutation(const struct connection *conn) { + const char *s = conn->mg_conn.request_method; + return s && (!strcmp(s, "PUT") || !strcmp(s, "DELETE") || + !strcmp(s, "MKCOL")); +} +#endif // MONGOOSE_NO_AUTH + +int parse_header(const char *str, int str_len, const char *var_name, char *buf, + size_t buf_size) { + int ch = ' ', len = 0, n = strlen(var_name); + const char *p, *end = str + str_len, *s = NULL; + + if (buf != NULL && buf_size > 0) buf[0] = '\0'; + + // Find where variable starts + for (s = str; s != NULL && s + n < end; s++) { + if ((s == str || s[-1] == ' ' || s[-1] == ',') && s[n] == '=' && + !memcmp(s, var_name, n)) break; + } + + if (s != NULL && &s[n + 1] < end) { + s += n + 1; + if (*s == '"' || *s == '\'') ch = *s++; + p = s; + while (p < end && p[0] != ch && p[0] != ',' && len < (int) buf_size) { + if (p[0] == '\\' && p[1] == ch) p++; + buf[len++] = *p++; + } + if (len >= (int) buf_size || (ch != ' ' && *p != ch)) { + len = 0; + } else { + if (len > 0 && s[len - 1] == ',') len--; + if (len > 0 && s[len - 1] == ';') len--; + buf[len] = '\0'; + } + } + + return len; +} + +int mg_parse_header(const char *s, const char *var_name, char *buf, + size_t buf_size) { + return parse_header(s, s == NULL ? 0 : strlen(s), var_name, buf, buf_size); +} + +#ifdef MONGOOSE_USE_LUA +#include "lua_5.2.1.h" + +#ifdef _WIN32 +static void *mmap(void *addr, int64_t len, int prot, int flags, int fd, + int offset) { + HANDLE fh = (HANDLE) _get_osfhandle(fd); + HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0); + void *p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, (size_t) len); + CloseHandle(mh); + return p; +} +#define munmap(x, y) UnmapViewOfFile(x) +#define MAP_FAILED NULL +#define MAP_PRIVATE 0 +#define PROT_READ 0 +#else +#include <sys/mman.h> +#endif + +static void reg_string(struct lua_State *L, const char *name, const char *val) { + lua_pushstring(L, name); + lua_pushstring(L, val); + lua_rawset(L, -3); +} + +static void reg_int(struct lua_State *L, const char *name, int val) { + lua_pushstring(L, name); + lua_pushinteger(L, val); + lua_rawset(L, -3); +} + +static void reg_function(struct lua_State *L, const char *name, + lua_CFunction func, struct mg_connection *conn) { + lua_pushstring(L, name); + lua_pushlightuserdata(L, conn); + lua_pushcclosure(L, func, 1); + lua_rawset(L, -3); +} + +static int lua_write(lua_State *L) { + int i, num_args; + const char *str; + size_t size; + struct mg_connection *conn = (struct mg_connection *) + lua_touserdata(L, lua_upvalueindex(1)); + + num_args = lua_gettop(L); + for (i = 1; i <= num_args; i++) { + if (lua_isstring(L, i)) { + str = lua_tolstring(L, i, &size); + mg_write(conn, str, size); + } + } + + return 0; +} + +static int lsp_sock_close(lua_State *L) { + if (lua_gettop(L) > 0 && lua_istable(L, -1)) { + lua_getfield(L, -1, "sock"); + closesocket((sock_t) lua_tonumber(L, -1)); + } else { + return luaL_error(L, "invalid :close() call"); + } + return 1; +} + +static int lsp_sock_recv(lua_State *L) { + char buf[2000]; + int n; + + if (lua_gettop(L) > 0 && lua_istable(L, -1)) { + lua_getfield(L, -1, "sock"); + n = recv((sock_t) lua_tonumber(L, -1), buf, sizeof(buf), 0); + if (n <= 0) { + lua_pushnil(L); + } else { + lua_pushlstring(L, buf, n); + } + } else { + return luaL_error(L, "invalid :close() call"); + } + return 1; +} + +static int lsp_sock_send(lua_State *L) { + const char *buf; + size_t len, sent = 0; + int n, sock; + + if (lua_gettop(L) > 1 && lua_istable(L, -2) && lua_isstring(L, -1)) { + buf = lua_tolstring(L, -1, &len); + lua_getfield(L, -2, "sock"); + sock = (int) lua_tonumber(L, -1); + while (sent < len) { + if ((n = send(sock, buf + sent, len - sent, 0)) <= 0) break; + sent += n; + } + lua_pushnumber(L, sent); + } else { + return luaL_error(L, "invalid :close() call"); + } + return 1; +} + +static const struct luaL_Reg luasocket_methods[] = { + {"close", lsp_sock_close}, + {"send", lsp_sock_send}, + {"recv", lsp_sock_recv}, + {NULL, NULL} +}; + +static sock_t conn2(const char *host, int port) { + struct sockaddr_in sin; + struct hostent *he = NULL; + sock_t sock = INVALID_SOCKET; + + if (host != NULL && + (he = gethostbyname(host)) != NULL && + (sock = socket(PF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) { + set_close_on_exec(sock); + sin.sin_family = AF_INET; + sin.sin_port = htons((uint16_t) port); + sin.sin_addr = * (struct in_addr *) he->h_addr_list[0]; + if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0) { + closesocket(sock); + sock = INVALID_SOCKET; + } + } + return sock; +} + +static int lsp_connect(lua_State *L) { + sock_t sock; + + if (lua_isstring(L, -2) && lua_isnumber(L, -1)) { + sock = conn2(lua_tostring(L, -2), (int) lua_tonumber(L, -1)); + if (sock == INVALID_SOCKET) { + lua_pushnil(L); + } else { + lua_newtable(L); + reg_int(L, "sock", sock); + reg_string(L, "host", lua_tostring(L, -4)); + luaL_getmetatable(L, "luasocket"); + lua_setmetatable(L, -2); + } + } else { + return luaL_error(L, "connect(host,port): invalid parameter given."); + } + return 1; +} + +static void prepare_lua_environment(struct mg_connection *ri, lua_State *L) { + extern void luaL_openlibs(lua_State *); + int i; + + luaL_openlibs(L); +#ifdef MONGOOSE_USE_LUA_SQLITE3 + { extern int luaopen_lsqlite3(lua_State *); luaopen_lsqlite3(L); } +#endif + + luaL_newmetatable(L, "luasocket"); + lua_pushliteral(L, "__index"); + luaL_newlib(L, luasocket_methods); + lua_rawset(L, -3); + lua_pop(L, 1); + lua_register(L, "connect", lsp_connect); + + if (ri == NULL) return; + + // Register mg module + lua_newtable(L); + reg_function(L, "write", lua_write, ri); + + // Export request_info + lua_pushstring(L, "request_info"); + lua_newtable(L); + reg_string(L, "request_method", ri->request_method); + reg_string(L, "uri", ri->uri); + reg_string(L, "http_version", ri->http_version); + reg_string(L, "query_string", ri->query_string); + reg_string(L, "remote_ip", ri->remote_ip); + reg_int(L, "remote_port", ri->remote_port); + lua_pushstring(L, "content"); + lua_pushlstring(L, ri->content == NULL ? "" : ri->content, 0); + lua_rawset(L, -3); + reg_int(L, "content_len", ri->content_len); + reg_int(L, "num_headers", ri->num_headers); + lua_pushstring(L, "http_headers"); + lua_newtable(L); + for (i = 0; i < ri->num_headers; i++) { + reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value); + } + lua_rawset(L, -3); + lua_rawset(L, -3); + + lua_setglobal(L, "mg"); + + // Register default mg.onerror function + (void) luaL_dostring(L, "mg.onerror = function(e) mg.write('\\nLua " + "error:\\n', debug.traceback(e, 1)) end"); +} + +static int lua_error_handler(lua_State *L) { + const char *error_msg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "?\n"; + + lua_getglobal(L, "mg"); + if (!lua_isnil(L, -1)) { + lua_getfield(L, -1, "write"); // call mg.write() + lua_pushstring(L, error_msg); + lua_pushliteral(L, "\n"); + lua_call(L, 2, 0); + (void) luaL_dostring(L, "mg.write(debug.traceback(), '\\n')"); + } else { + printf("Lua error: [%s]\n", error_msg); + (void) luaL_dostring(L, "print(debug.traceback(), '\\n')"); + } + // TODO(lsm): leave the stack balanced + + return 0; +} + +static void lsp(struct connection *conn, const char *p, int len, lua_State *L) { + int i, j, pos = 0; + + for (i = 0; i < len; i++) { + if (p[i] == '<' && p[i + 1] == '?') { + for (j = i + 1; j < len ; j++) { + if (p[j] == '?' && p[j + 1] == '>') { + mg_write(&conn->mg_conn, p + pos, i - pos); + if (luaL_loadbuffer(L, p + (i + 2), j - (i + 2), "") == LUA_OK) { + lua_pcall(L, 0, LUA_MULTRET, 0); + } + pos = j + 2; + i = pos - 1; + break; + } + } + } + } + if (i > pos) mg_write(&conn->mg_conn, p + pos, i - pos); +} + +static void handle_lsp_request(struct connection *conn, const char *path, + file_stat_t *st) { + void *p = NULL; + lua_State *L = NULL; + FILE *fp = NULL; + + if ((fp = fopen(path, "r")) == NULL || + (p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, + fileno(fp), 0)) == MAP_FAILED || + (L = luaL_newstate()) == NULL) { + send_http_error(conn, 500, "mmap(%s): %s", path, strerror(errno)); + } else { + // We're not sending HTTP headers here, Lua page must do it. + prepare_lua_environment(&conn->mg_conn, L); + lua_pushcclosure(L, &lua_error_handler, 0); + lua_pushglobaltable(L); + lsp(conn, p, (int) st->st_size, L); + close_local_endpoint(conn); + } + + if (L != NULL) lua_close(L); + if (p != NULL) munmap(p, st->st_size); + if (fp != NULL) fclose(fp); +} +#endif // MONGOOSE_USE_LUA + +static void open_local_endpoint(struct connection *conn, int skip_user) { +#ifndef MONGOOSE_NO_FILESYSTEM + static const char lua_pat[] = LUA_SCRIPT_PATTERN; + file_stat_t st; + char path[MAX_PATH_SIZE]; + int exists = 0, is_directory = 0; +#ifndef MONGOOSE_NO_CGI + const char *cgi_pat = conn->server->config_options[CGI_PATTERN]; +#else + const char *cgi_pat = DEFAULT_CGI_PATTERN; +#endif +#ifndef MONGOOSE_NO_DIRECTORY_LISTING + const char *dir_lst = conn->server->config_options[ENABLE_DIRECTORY_LISTING]; +#else + const char *dir_lst = "yes"; +#endif +#endif + +#ifndef MONGOOSE_NO_AUTH + // Call auth handler + if (conn->server->auth_handler != NULL && + conn->server->auth_handler(&conn->mg_conn) == MG_AUTH_FAIL) { + mg_send_digest_auth_request(&conn->mg_conn); + return; + } +#endif + + // Call URI handler if one is registered for this URI + if (skip_user == 0 && conn->server->request_handler != NULL) { + conn->endpoint_type = EP_USER; +#if MONGOOSE_USE_POST_SIZE_LIMIT > 1 + { + const char *cl = mg_get_header(&conn->mg_conn, "Content-Length"); + if (!strcmp(conn->mg_conn.request_method, "POST") && + (cl == NULL || to64(cl) > MONGOOSE_USE_POST_SIZE_LIMIT)) { + send_http_error(conn, 500, "POST size > %zu", + (size_t) MONGOOSE_USE_POST_SIZE_LIMIT); + } + } +#endif + return; + } + +#ifdef MONGOOSE_NO_FILESYSTEM + if (!strcmp(conn->mg_conn.request_method, "OPTIONS")) { + send_options(conn); + } else { + send_http_error(conn, 404, NULL); + } +#else + exists = convert_uri_to_file_name(conn, path, sizeof(path), &st); + is_directory = S_ISDIR(st.st_mode); + + if (!strcmp(conn->mg_conn.request_method, "OPTIONS")) { + send_options(conn); + } else if (conn->server->config_options[DOCUMENT_ROOT] == NULL) { + send_http_error(conn, 404, NULL); +#ifndef MONGOOSE_NO_AUTH + } else if ((!is_dav_mutation(conn) && !is_authorized(conn, path)) || + (is_dav_mutation(conn) && !is_authorized_for_dav(conn))) { + mg_send_digest_auth_request(&conn->mg_conn); + close_local_endpoint(conn); +#endif +#ifndef MONGOOSE_NO_DAV + } else if (!strcmp(conn->mg_conn.request_method, "PROPFIND")) { + handle_propfind(conn, path, &st); + } else if (!strcmp(conn->mg_conn.request_method, "MKCOL")) { + handle_mkcol(conn, path); + } else if (!strcmp(conn->mg_conn.request_method, "DELETE")) { + handle_delete(conn, path); + } else if (!strcmp(conn->mg_conn.request_method, "PUT")) { + handle_put(conn, path); +#endif + } else if (!exists || must_hide_file(conn, path)) { + send_http_error(conn, 404, NULL); + } else if (is_directory && + conn->mg_conn.uri[strlen(conn->mg_conn.uri) - 1] != '/') { + conn->mg_conn.status_code = 301; + mg_printf(&conn->mg_conn, "HTTP/1.1 301 Moved Permanently\r\n" + "Location: %s/\r\n\r\n", conn->mg_conn.uri); + close_local_endpoint(conn); + } else if (is_directory && !find_index_file(conn, path, sizeof(path), &st)) { + if (!mg_strcasecmp(dir_lst, "yes")) { +#ifndef MONGOOSE_NO_DIRECTORY_LISTING + send_directory_listing(conn, path); +#else + send_http_error(conn, 501, NULL); +#endif + } else { + send_http_error(conn, 403, NULL); + } + } else if (match_prefix(lua_pat, sizeof(lua_pat) - 1, path) > 0) { +#ifdef MONGOOSE_USE_LUA + handle_lsp_request(conn, path, &st); +#else + send_http_error(conn, 501, NULL); +#endif + } else if (match_prefix(cgi_pat, strlen(cgi_pat), path) > 0) { +#if !defined(MONGOOSE_NO_CGI) + open_cgi_endpoint(conn, path); +#else + send_http_error(conn, 501, NULL); +#endif // !MONGOOSE_NO_CGI + } else if (is_not_modified(conn, &st)) { + send_http_error(conn, 304, NULL); + } else if ((conn->endpoint.fd = open(path, O_RDONLY | O_BINARY)) != -1) { + // O_BINARY is required for Windows, otherwise in default text mode + // two bytes \r\n will be read as one. + open_file_endpoint(conn, path, &st); + } else { + send_http_error(conn, 404, NULL); + } +#endif // MONGOOSE_NO_FILESYSTEM +} + +static void send_continue_if_expected(struct connection *conn) { + static const char expect_response[] = "HTTP/1.1 100 Continue\r\n\r\n"; + const char *expect_hdr = mg_get_header(&conn->mg_conn, "Expect"); + + if (expect_hdr != NULL && !mg_strcasecmp(expect_hdr, "100-continue")) { + spool(&conn->remote_iobuf, expect_response, sizeof(expect_response) - 1); + } +} + +static int is_valid_uri(const char *uri) { + // Conform to http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2 + // URI can be an asterisk (*) or should start with slash. + return uri[0] == '/' || (uri[0] == '*' && uri[1] == '\0'); +} + +static void try_http_parse_and_set_content_length(struct connection *conn) { + struct iobuf *io = &conn->local_iobuf; + + if (conn->request_len == 0 && + (conn->request_len = get_request_len(io->buf, io->len)) > 0) { + // If request is buffered in, remove it from the iobuf. This is because + // iobuf could be reallocated, and pointers in parsed request could + // become invalid. + conn->request = (char *) malloc(conn->request_len); + memcpy(conn->request, io->buf, conn->request_len); + DBG(("%p [%.*s]", conn, conn->request_len, conn->request)); + discard_leading_iobuf_bytes(io, conn->request_len); + conn->request_len = parse_http_message(conn->request, conn->request_len, + &conn->mg_conn); + if (conn->request_len > 0) { + const char *cl_hdr = mg_get_header(&conn->mg_conn, "Content-Length"); + conn->cl = cl_hdr == NULL ? 0 : to64(cl_hdr); + conn->mg_conn.content_len = (long int) conn->cl; + } + } +} + +static void process_request(struct connection *conn) { + struct iobuf *io = &conn->local_iobuf; + + try_http_parse_and_set_content_length(conn); + DBG(("%p %d %d %d [%.*s]", conn, conn->request_len, io->len, conn->flags, + io->len, io->buf)); + if (conn->request_len < 0 || + (conn->request_len > 0 && !is_valid_uri(conn->mg_conn.uri))) { + send_http_error(conn, 400, NULL); + } else if (conn->request_len == 0 && io->len > MAX_REQUEST_SIZE) { + send_http_error(conn, 413, NULL); + } else if (conn->request_len > 0 && + strcmp(conn->mg_conn.http_version, "1.0") != 0 && + strcmp(conn->mg_conn.http_version, "1.1") != 0) { + send_http_error(conn, 505, NULL); + } else if (conn->request_len > 0 && conn->endpoint_type == EP_NONE) { +#ifndef MONGOOSE_NO_WEBSOCKET + send_websocket_handshake_if_requested(&conn->mg_conn); +#endif + send_continue_if_expected(conn); + open_local_endpoint(conn, 0); + } + +#ifndef MONGOOSE_NO_CGI + if (conn->endpoint_type == EP_CGI && io->len > 0) { + forward_post_data(conn); + } +#endif + if (conn->endpoint_type == EP_USER) { + call_request_handler_if_data_is_buffered(conn); + } +#ifndef MONGOOSE_NO_DAV + if (conn->endpoint_type == EP_PUT && io->len > 0) { + forward_put_data(conn); + } +#endif +} + +static void call_http_client_handler(struct connection *conn, int code) { + conn->mg_conn.status_code = code; + // For responses without Content-Lengh, use the whole buffer + if (conn->cl == 0 && code == MG_DOWNLOAD_SUCCESS) { + conn->mg_conn.content_len = conn->local_iobuf.len; + } + conn->mg_conn.content = conn->local_iobuf.buf; + if (conn->handler(&conn->mg_conn) || code == MG_CONNECT_FAILURE || + code == MG_DOWNLOAD_FAILURE) { + conn->flags |= CONN_CLOSE; + } + discard_leading_iobuf_bytes(&conn->local_iobuf, conn->mg_conn.content_len); + conn->flags = conn->mg_conn.status_code = 0; + conn->cl = conn->num_bytes_sent = conn->request_len = 0; + free(conn->request); + conn->request = NULL; +} + +static void process_response(struct connection *conn) { + struct iobuf *io = &conn->local_iobuf; + + try_http_parse_and_set_content_length(conn); + DBG(("%p %d %d [%.*s]", conn, conn->request_len, io->len, + io->len > 40 ? 40 : io->len, io->buf)); + if (conn->request_len < 0 || + (conn->request_len == 0 && io->len > MAX_REQUEST_SIZE)) { + call_http_client_handler(conn, MG_DOWNLOAD_FAILURE); + } + if (io->len >= conn->cl) { + call_http_client_handler(conn, MG_DOWNLOAD_SUCCESS); + } +} + +static void read_from_socket(struct connection *conn) { + char buf[IOBUF_SIZE]; + int n = 0; + + if (conn->endpoint_type == EP_CLIENT && conn->flags & CONN_CONNECTING) { + callback_http_client_on_connect(conn); + return; + } + +#ifdef MONGOOSE_USE_SSL + if (conn->ssl != NULL) { + if (conn->flags & CONN_SSL_HANDS_SHAKEN) { + n = SSL_read(conn->ssl, buf, sizeof(buf)); + } else { + if (SSL_accept(conn->ssl) == 1) { + conn->flags |= CONN_SSL_HANDS_SHAKEN; + } + return; + } + } else +#endif + { + n = recv(conn->client_sock, buf, sizeof(buf), 0); + } + + DBG(("%p %d %d (1)", conn, n, conn->flags)); + if (is_error(n)) { + if (conn->endpoint_type == EP_CLIENT && conn->local_iobuf.len > 0) { + call_http_client_handler(conn, MG_DOWNLOAD_SUCCESS); + } + conn->flags |= CONN_CLOSE; + } else if (n > 0) { + spool(&conn->local_iobuf, buf, n); + if (conn->endpoint_type == EP_CLIENT) { + process_response(conn); + } else { + process_request(conn); + } + } + DBG(("%p %d %d (2)", conn, n, conn->flags)); +} + +int mg_connect(struct mg_server *server, const char *host, int port, + int use_ssl, mg_handler_t handler, void *param) { + sock_t sock = INVALID_SOCKET; + struct sockaddr_in sin; + struct hostent *he = NULL; + struct connection *conn = NULL; + int connect_ret_val; + + if (host == NULL || (he = gethostbyname(host)) == NULL || + (sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) return 0; +#ifndef MONGOOSE_USE_SSL + if (use_ssl) return 0; +#endif + + sin.sin_family = AF_INET; + sin.sin_port = htons((uint16_t) port); + sin.sin_addr = * (struct in_addr *) he->h_addr_list[0]; + set_non_blocking_mode(sock); + + connect_ret_val = connect(sock, (struct sockaddr *) &sin, sizeof(sin)); + if (is_error(connect_ret_val)) { + return 0; + } else if ((conn = (struct connection *) calloc(1, sizeof(*conn))) == NULL) { + closesocket(sock); + return 0; + } + + conn->server = server; + conn->client_sock = sock; + conn->endpoint_type = EP_CLIENT; + conn->handler = handler; + conn->mg_conn.server_param = server->server_data; + conn->mg_conn.connection_param = param; + conn->birth_time = conn->last_activity_time = time(NULL); + conn->flags = CONN_CONNECTING; + conn->mg_conn.status_code = MG_CONNECT_FAILURE; +#ifdef MONGOOSE_USE_SSL + if (use_ssl && (conn->ssl = SSL_new(server->client_ssl_ctx)) != NULL) { + SSL_set_fd(conn->ssl, sock); + } +#endif + LINKED_LIST_ADD_TO_FRONT(&server->active_connections, &conn->link); + DBG(("%p %s:%d", conn, host, port)); + + return 1; +} + +#ifndef MONGOOSE_NO_LOGGING +static void log_header(const struct mg_connection *conn, const char *header, + FILE *fp) { + const char *header_value; + + if ((header_value = mg_get_header(conn, header)) == NULL) { + (void) fprintf(fp, "%s", " -"); + } else { + (void) fprintf(fp, " \"%s\"", header_value); + } +} + +static void log_access(const struct connection *conn, const char *path) { + const struct mg_connection *c = &conn->mg_conn; + FILE *fp = (path == NULL) ? NULL : fopen(path, "a+"); + char date[64], user[100]; + + if (fp == NULL) return; + strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z", + localtime(&conn->birth_time)); + + flockfile(fp); + mg_parse_header(mg_get_header(&conn->mg_conn, "Authorization"), "username", + user, sizeof(user)); + fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT, + c->remote_ip, user[0] == '\0' ? "-" : user, date, + c->request_method ? c->request_method : "-", + c->uri ? c->uri : "-", c->http_version, + c->status_code, conn->num_bytes_sent); + log_header(c, "Referer", fp); + log_header(c, "User-Agent", fp); + fputc('\n', fp); + fflush(fp); + + funlockfile(fp); + fclose(fp); +} +#endif + +static void close_local_endpoint(struct connection *conn) { + struct mg_connection *c = &conn->mg_conn; + // Must be done before free() + int keep_alive = should_keep_alive(&conn->mg_conn) && + (conn->endpoint_type == EP_FILE || conn->endpoint_type == EP_USER); + DBG(("%p %d %d %d", conn, conn->endpoint_type, keep_alive, conn->flags)); + + switch (conn->endpoint_type) { + case EP_PUT: close(conn->endpoint.fd); break; + case EP_FILE: close(conn->endpoint.fd); break; + case EP_CGI: closesocket(conn->endpoint.cgi_sock); break; + default: break; + } + +#ifndef MONGOOSE_NO_LOGGING + if (c->status_code > 0 && conn->endpoint_type != EP_CLIENT && + c->status_code != 400) { + log_access(conn, conn->server->config_options[ACCESS_LOG_FILE]); + } +#endif + + // Gobble possible POST data sent to the URI handler + discard_leading_iobuf_bytes(&conn->local_iobuf, conn->mg_conn.content_len); + conn->endpoint_type = EP_NONE; + conn->cl = conn->num_bytes_sent = conn->request_len = conn->flags = 0; + c->request_method = c->uri = c->http_version = c->query_string = NULL; + c->num_headers = c->status_code = c->is_websocket = c->content_len = 0; + free(conn->request); + conn->request = NULL; + + if (keep_alive) { + process_request(conn); // Can call us recursively if pipelining is used + } else { + conn->flags |= conn->remote_iobuf.len == 0 ? CONN_CLOSE : CONN_SPOOL_DONE; + } +} + +static void transfer_file_data(struct connection *conn) { + char buf[IOBUF_SIZE]; + int n = read(conn->endpoint.fd, buf, conn->cl < (int64_t) sizeof(buf) ? + (int) conn->cl : (int) sizeof(buf)); + + if (is_error(n)) { + close_local_endpoint(conn); + } else if (n > 0) { + conn->cl -= n; + spool(&conn->remote_iobuf, buf, n); + if (conn->cl <= 0) { + close_local_endpoint(conn); + } + } +} + +static void execute_iteration(struct mg_server *server) { + struct ll *lp, *tmp; + struct connection *conn; + union { mg_handler_t f; void *p; } msg[2]; + + recv(server->ctl[1], (void *) msg, sizeof(msg), 0); + LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { + conn = LINKED_LIST_ENTRY(lp, struct connection, link); + conn->mg_conn.connection_param = msg[1].p; + msg[0].f(&conn->mg_conn); + } +} + +void add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) { + FD_SET(sock, set); + if (sock > *max_fd) { + *max_fd = sock; + } +} + +unsigned int mg_poll_server(struct mg_server *server, int milliseconds) { + struct ll *lp, *tmp; + struct connection *conn; + struct timeval tv; + fd_set read_set, write_set; + sock_t max_fd = -1; + time_t current_time = time(NULL), expire_time = current_time - + MONGOOSE_USE_IDLE_TIMEOUT_SECONDS; + + if (server->listening_sock == INVALID_SOCKET) return 0; + + FD_ZERO(&read_set); + FD_ZERO(&write_set); + add_to_set(server->listening_sock, &read_set, &max_fd); + add_to_set(server->ctl[1], &read_set, &max_fd); + + LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { + conn = LINKED_LIST_ENTRY(lp, struct connection, link); + add_to_set(conn->client_sock, &read_set, &max_fd); + if (conn->endpoint_type == EP_CLIENT && (conn->flags & CONN_CONNECTING)) { + add_to_set(conn->client_sock, &write_set, &max_fd); + } + if (conn->endpoint_type == EP_FILE) { + transfer_file_data(conn); + } else if (conn->endpoint_type == EP_CGI) { + add_to_set(conn->endpoint.cgi_sock, &read_set, &max_fd); + } + if (conn->remote_iobuf.len > 0 && !(conn->flags & CONN_BUFFER)) { + add_to_set(conn->client_sock, &write_set, &max_fd); + } else if (conn->flags & CONN_CLOSE) { + close_conn(conn); + } + } + + tv.tv_sec = milliseconds / 1000; + tv.tv_usec = (milliseconds % 1000) * 1000; + + if (select(max_fd + 1, &read_set, &write_set, NULL, &tv) > 0) { + if (FD_ISSET(server->ctl[1], &read_set)) { + execute_iteration(server); + } + + // Accept new connections + if (FD_ISSET(server->listening_sock, &read_set)) { + while ((conn = accept_new_connection(server)) != NULL) { + conn->birth_time = conn->last_activity_time = current_time; + } + } + + // Read/write from clients + LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { + conn = LINKED_LIST_ENTRY(lp, struct connection, link); + if (FD_ISSET(conn->client_sock, &read_set)) { + conn->last_activity_time = current_time; + read_from_socket(conn); + } +#ifndef MONGOOSE_NO_CGI + if (conn->endpoint_type == EP_CGI && + FD_ISSET(conn->endpoint.cgi_sock, &read_set)) { + read_from_cgi(conn); + } +#endif + if (FD_ISSET(conn->client_sock, &write_set)) { + if (conn->endpoint_type == EP_CLIENT && + (conn->flags & CONN_CONNECTING)) { + read_from_socket(conn); + } else if (!(conn->flags & CONN_BUFFER)) { + conn->last_activity_time = current_time; + write_to_socket(conn); + } + } + } + } + + // Close expired connections and those that need to be closed + LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { + conn = LINKED_LIST_ENTRY(lp, struct connection, link); + if (conn->mg_conn.is_websocket) { + ping_idle_websocket_connection(conn, current_time); + } + if (conn->flags & CONN_LONG_RUNNING) { + conn->mg_conn.wsbits = conn->flags & CONN_CLOSE ? 1 : 0; + if (call_request_handler(conn) == MG_REQUEST_PROCESSED) { + conn->flags |= CONN_CLOSE; + } + } + if (conn->flags & CONN_CLOSE || conn->last_activity_time < expire_time) { + close_conn(conn); + } + } + + return (unsigned int) current_time; +} + +void mg_destroy_server(struct mg_server **server) { + int i; + struct ll *lp, *tmp; + + if (server != NULL && *server != NULL) { + struct mg_server *s = *server; + // Do one last poll, see https://github.com/cesanta/mongoose/issues/286 + mg_poll_server(s, 0); + closesocket(s->listening_sock); + closesocket(s->ctl[0]); + closesocket(s->ctl[1]); + LINKED_LIST_FOREACH(&s->active_connections, lp, tmp) { + close_conn(LINKED_LIST_ENTRY(lp, struct connection, link)); + } + for (i = 0; i < (int) ARRAY_SIZE(s->config_options); i++) { + free(s->config_options[i]); // It is OK to free(NULL) + } +#ifdef MONGOOSE_USE_SSL + if (s->ssl_ctx != NULL) SSL_CTX_free((*server)->ssl_ctx); + if (s->client_ssl_ctx != NULL) SSL_CTX_free(s->client_ssl_ctx); +#endif + free(s); + *server = NULL; + } +} + +// Apply function to all active connections. +void mg_iterate_over_connections(struct mg_server *server, mg_handler_t handler, + void *param) { + // Send closure (function + parameter) to the IO thread to execute + union { mg_handler_t f; void *p; } msg[2]; + msg[0].f = handler; + msg[1].p = param; + send(server->ctl[0], (void *) msg, sizeof(msg), 0); +} + +static int get_var(const char *data, size_t data_len, const char *name, + char *dst, size_t dst_len) { + const char *p, *e, *s; + size_t name_len; + int len; + + if (dst == NULL || dst_len == 0) { + len = -2; + } else if (data == NULL || name == NULL || data_len == 0) { + len = -1; + dst[0] = '\0'; + } else { + name_len = strlen(name); + e = data + data_len; + len = -1; + dst[0] = '\0'; + + // data is "var1=val1&var2=val2...". Find variable first + for (p = data; p + name_len < e; p++) { + if ((p == data || p[-1] == '&') && p[name_len] == '=' && + !mg_strncasecmp(name, p, name_len)) { + + // Point p to variable value + p += name_len + 1; + + // Point s to the end of the value + s = (const char *) memchr(p, '&', (size_t)(e - p)); + if (s == NULL) { + s = e; + } + assert(s >= p); + + // Decode variable into destination buffer + len = mg_url_decode(p, (size_t)(s - p), dst, dst_len, 1); + + // Redirect error code from -1 to -2 (destination buffer too small). + if (len == -1) { + len = -2; + } + break; + } + } + } + + return len; +} + +int mg_get_var(const struct mg_connection *conn, const char *name, + char *dst, size_t dst_len) { + int len = get_var(conn->query_string, conn->query_string == NULL ? 0 : + strlen(conn->query_string), name, dst, dst_len); + if (len < 0) { + len = get_var(conn->content, conn->content_len, name, dst, dst_len); + } + return len; +} + +static int get_line_len(const char *buf, int buf_len) { + int len = 0; + while (len < buf_len && buf[len] != '\n') len++; + return buf[len] == '\n' ? len + 1: -1; +} + +int mg_parse_multipart(const char *buf, int buf_len, + char *var_name, int var_name_len, + char *file_name, int file_name_len, + const char **data, int *data_len) { + static const char cd[] = "Content-Disposition: "; + //struct mg_connection c; + int hl, bl, n, ll, pos, cdl = sizeof(cd) - 1; + //char *p; + + if (buf == NULL || buf_len <= 0) return 0; + if ((hl = get_request_len(buf, buf_len)) <= 0) return 0; + if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0; + + // Get boundary length + bl = get_line_len(buf, buf_len); + + // Loop through headers, fetch variable name and file name + var_name[0] = file_name[0] = '\0'; + for (n = bl; (ll = get_line_len(buf + n, hl - n)) > 0; n += ll) { + if (mg_strncasecmp(cd, buf + n, cdl) == 0) { + parse_header(buf + n + cdl, ll - (cdl + 2), "name", + var_name, var_name_len); + parse_header(buf + n + cdl, ll - (cdl + 2), "filename", + file_name, file_name_len); + } + } + + // Scan body, search for terminating boundary + for (pos = hl; pos + (bl - 2) < buf_len; pos++) { + if (buf[pos] == '-' && !memcmp(buf, &buf[pos], bl - 2)) { + if (data_len != NULL) *data_len = (pos - 2) - hl; + if (data != NULL) *data = buf + hl; + return pos; + } + } + + return 0; +} + +const char **mg_get_valid_option_names(void) { + return static_config_options; +} + +static int get_option_index(const char *name) { + int i; + + for (i = 0; static_config_options[i * 2] != NULL; i++) { + if (strcmp(static_config_options[i * 2], name) == 0) { + return i; + } + } + return -1; +} + +static void set_default_option_values(char **opts) { + const char *value, **all_opts = mg_get_valid_option_names(); + int i; + + for (i = 0; all_opts[i * 2] != NULL; i++) { + value = all_opts[i * 2 + 1]; + if (opts[i] == NULL && value != NULL) { + opts[i] = mg_strdup(value); + } + } +} + +// Valid listening port spec is: [ip_address:]port, e.g. "80", "127.0.0.1:3128" +static int parse_port_string(const char *str, union socket_address *sa) { + unsigned int a, b, c, d, port; + int len = 0; +#ifdef MONGOOSE_USE_IPV6 + char buf[100]; +#endif + + // MacOS needs that. If we do not zero it, subsequent bind() will fail. + // Also, all-zeroes in the socket address means binding to all addresses + // for both IPv4 and IPv6 (INADDR_ANY and IN6ADDR_ANY_INIT). + memset(sa, 0, sizeof(*sa)); + sa->sin.sin_family = AF_INET; + + if (sscanf(str, "%u.%u.%u.%u:%u%n", &a, &b, &c, &d, &port, &len) == 5) { + // Bind to a specific IPv4 address, e.g. 192.168.1.5:8080 + sa->sin.sin_addr.s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d); + sa->sin.sin_port = htons((uint16_t) port); +#if defined(MONGOOSE_USE_IPV6) + } else if (sscanf(str, "[%49[^]]]:%u%n", buf, &port, &len) == 2 && + inet_pton(AF_INET6, buf, &sa->sin6.sin6_addr)) { + // IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 + sa->sin6.sin6_family = AF_INET6; + sa->sin6.sin6_port = htons((uint16_t) port); +#endif + } else if (sscanf(str, "%u%n", &port, &len) == 1) { + // If only port is specified, bind to IPv4, INADDR_ANY + sa->sin.sin_port = htons((uint16_t) port); + } else { + port = 0; // Parsing failure. Make port invalid. + } + + return port <= 0xffff && str[len] == '\0'; +} + +const char *mg_set_option(struct mg_server *server, const char *name, + const char *value) { + int ind = get_option_index(name); + const char *error_msg = NULL; + + if (ind < 0) { + error_msg = "No such option"; + } else { + if (server->config_options[ind] != NULL) { + free(server->config_options[ind]); + } + server->config_options[ind] = mg_strdup(value); + DBG(("%s [%s]", name, value)); + + if (ind == LISTENING_PORT) { + if (server->listening_sock != INVALID_SOCKET) { + closesocket(server->listening_sock); + } + parse_port_string(server->config_options[LISTENING_PORT], &server->lsa); + server->listening_sock = open_listening_socket(&server->lsa); + if (server->listening_sock == INVALID_SOCKET) { + error_msg = "Cannot bind to port"; + } else { + sockaddr_to_string(server->local_ip, sizeof(server->local_ip), + &server->lsa); + if (!strcmp(value, "0")) { + char buf[10]; + mg_snprintf(buf, sizeof(buf), "%d", + (int) ntohs(server->lsa.sin.sin_port)); + free(server->config_options[ind]); + server->config_options[ind] = mg_strdup(buf); + } + } +#ifndef _WIN32 + } else if (ind == RUN_AS_USER) { + struct passwd *pw; + if ((pw = getpwnam(value)) == NULL) { + error_msg = "Unknown user"; + } else if (setgid(pw->pw_gid) != 0) { + error_msg = "setgid() failed"; + } else if (setuid(pw->pw_uid) != 0) { + error_msg = "setuid() failed"; + } +#endif +#ifdef MONGOOSE_USE_SSL + } else if (ind == SSL_CERTIFICATE) { + //SSL_library_init(); + if ((server->ssl_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL) { + error_msg = "SSL_CTX_new() failed"; + } else if (SSL_CTX_use_certificate_file(server->ssl_ctx, value, 1) == 0 || + SSL_CTX_use_PrivateKey_file(server->ssl_ctx, value, 1) == 0) { + error_msg = "Cannot load PEM file"; + } else { + SSL_CTX_use_certificate_chain_file(server->ssl_ctx, value); + } +#endif + } + } + + return error_msg; +} + +void mg_set_request_handler(struct mg_server *server, mg_handler_t handler) { + server->request_handler = handler; +} + +void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) { + server->error_handler = handler; +} + +void mg_set_auth_handler(struct mg_server *server, mg_handler_t handler) { + server->auth_handler = handler; +} + +void mg_set_listening_socket(struct mg_server *server, int sock) { + if (server->listening_sock != INVALID_SOCKET) { + closesocket(server->listening_sock); + } + server->listening_sock = (sock_t) sock; +} + +int mg_get_listening_socket(struct mg_server *server) { + return server->listening_sock; +} + +const char *mg_get_option(const struct mg_server *server, const char *name) { + const char **opts = (const char **) server->config_options; + int i = get_option_index(name); + return i == -1 ? NULL : opts[i] == NULL ? "" : opts[i]; +} + +struct mg_server *mg_create_server(void *server_data) { + struct mg_server *server = (struct mg_server *) calloc(1, sizeof(*server)); + +#ifdef _WIN32 + WSADATA data; + WSAStartup(MAKEWORD(2, 2), &data); +#else + // Ignore SIGPIPE signal, so if browser cancels the request, it + // won't kill the whole process. + signal(SIGPIPE, SIG_IGN); +#endif + + LINKED_LIST_INIT(&server->active_connections); + + // Create control socket pair. Do it in a loop to protect from + // interrupted syscalls in mg_socketpair(). + do { + mg_socketpair(server->ctl); + } while (server->ctl[0] == INVALID_SOCKET); + +#ifdef MONGOOSE_USE_SSL + SSL_library_init(); + server->client_ssl_ctx = SSL_CTX_new(SSLv23_client_method()); +#endif + + server->server_data = server_data; + server->listening_sock = INVALID_SOCKET; + set_default_option_values(server->config_options); + + return server; +} diff --git a/3rdparty/mongoose/mongoose.h b/3rdparty/mongoose/mongoose.h new file mode 100644 index 000000000..5822b8153 --- /dev/null +++ b/3rdparty/mongoose/mongoose.h @@ -0,0 +1,123 @@ +// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> +// Copyright (c) 2013-2014 Cesanta Software Limited +// All rights reserved +// +// This library is dual-licensed: you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. For the terms of this +// license, see <http://www.gnu.org/licenses/>. +// +// You are free to use this library under the terms of the GNU General +// Public License, but WITHOUT ANY WARRANTY; without even the implied +// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// Alternatively, you can license this library under a commercial +// license, as set out in <http://cesanta.com/>. +// +// NOTE: Detailed API documentation is at http://cesanta.com/#docs + +#ifndef MONGOOSE_HEADER_INCLUDED +#define MONGOOSE_HEADER_INCLUDED + +#define MONGOOSE_VERSION "5.2" + +#include <stdio.h> // required for FILE +#include <stddef.h> // required for size_t + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// This structure contains information about HTTP request. +struct mg_connection { + const char *request_method; // "GET", "POST", etc + const char *uri; // URL-decoded URI + const char *http_version; // E.g. "1.0", "1.1" + const char *query_string; // URL part after '?', not including '?', or NULL + + char remote_ip[48]; // Max IPv6 string length is 45 characters + const char *local_ip; // Local IP address + unsigned short remote_port; // Client's port + unsigned short local_port; // Local port number + + int num_headers; // Number of HTTP headers + struct mg_header { + const char *name; // HTTP header name + const char *value; // HTTP header value + } http_headers[30]; + + char *content; // POST (or websocket message) data, or NULL + size_t content_len; // content length + + int is_websocket; // Connection is a websocket connection + int status_code; // HTTP status code for HTTP error handler + 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 +}; + +struct mg_server; // Opaque structure describing server instance +typedef int (*mg_handler_t)(struct mg_connection *); + +// Server management functions +struct mg_server *mg_create_server(void *server_param); +void mg_destroy_server(struct mg_server **); +const char *mg_set_option(struct mg_server *, const char *opt, const char *val); +unsigned int mg_poll_server(struct mg_server *, int milliseconds); +void mg_set_request_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 *); + +// Connection management functions +void mg_send_status(struct mg_connection *, int status_code); +void mg_send_header(struct mg_connection *, const char *name, const char *val); +void mg_send_data(struct mg_connection *, const void *data, int data_len); +void mg_printf_data(struct mg_connection *, const char *format, ...); + +int mg_websocket_write(struct mg_connection *, int opcode, + const char *data, size_t data_len); + +// Deprecated in favor of mg_send_* interface +int mg_write(struct mg_connection *, const void *buf, int len); +int mg_printf(struct mg_connection *conn, const char *fmt, ...); + +const char *mg_get_header(const struct mg_connection *, const char *name); +const char *mg_get_mime_type(const char *name, const char *default_mime_type); +int mg_get_var(const struct mg_connection *conn, const char *var_name, + char *buf, size_t buf_len); +int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); +int mg_parse_multipart(const char *buf, int buf_len, + char *var_name, int var_name_len, + char *file_name, int file_name_len, + const char **data, int *data_len); + +// Utility functions +void *mg_start_thread(void *(*func)(void *), void *param); +char *mg_md5(char buf[33], ...); +int mg_authorize_digest(struct mg_connection *c, FILE *fp); + +// Callback function return codes +enum { MG_REQUEST_NOT_PROCESSED, MG_REQUEST_PROCESSED, MG_REQUEST_CALL_AGAIN }; +enum { MG_AUTH_FAIL, MG_AUTH_OK }; +enum { MG_ERROR_NOT_PROCESSED, MG_ERROR_PROCESSED }; +enum { MG_CLIENT_CONTINUE, MG_CLIENT_CLOSE }; + +// HTTP client events +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); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // MONGOOSE_HEADER_INCLUDED diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index bf4b7f350..a52216acc 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -121,6 +121,7 @@ #include <Cockpit/cockpitDisplayManager.hxx> #include <Network/HTTPClient.hxx> #include <Network/fgcom.hxx> +#include <Network/http/httpd.hxx> #include <Viewer/CameraGroup.hxx> @@ -767,6 +768,12 @@ void fgCreateSubsystems(bool duringReset) { globals->add_subsystem("fgcom", new FGCom); #endif + { + SGSubsystem * httpd = flightgear::http::FGHttpd::createInstance( fgGetNode(flightgear::http::PROPERTY_ROOT) ); + if( NULL != httpd ) + globals->add_subsystem("httpd", httpd ); + } + //////////////////////////////////////////////////////////////////// // Initialize the lighting subsystem. //////////////////////////////////////////////////////////////////// diff --git a/src/Main/fg_io.cxx b/src/Main/fg_io.cxx index 059bb97a2..de274afa2 100644 --- a/src/Main/fg_io.cxx +++ b/src/Main/fg_io.cxx @@ -48,7 +48,7 @@ #include <Network/AV400Sim.hxx> #include <Network/AV400WSim.hxx> #include <Network/garmin.hxx> -#include <Network/httpd.hxx> +#include <Network/http/httpd.hxx> #include <Network/igc.hxx> #include <Network/jpg-httpd.hxx> #include <Network/joyclient.hxx> @@ -149,10 +149,6 @@ FGIO::parse_port_config( const string& config ) } else if ( protocol == "igc" ) { IGCProtocol *igc = new IGCProtocol; io = igc; - } else if ( protocol == "httpd" ) { - // determine port - string port = tokens[1]; - return new FGHttpd( atoi(port.c_str()) ); } else if ( protocol == "jpg-httpd" ) { // determine port int port = simgear::strutils::to_int(tokens[1]); diff --git a/src/Main/options.cxx b/src/Main/options.cxx index c1996bbef..694ffd3e2 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -65,6 +65,7 @@ #include <Viewer/viewer.hxx> #include <Viewer/viewmgr.hxx> #include <Environment/presets.hxx> +#include <Network/http/httpd.hxx> #include "AircraftDirVisitorBase.hxx" #include <osg/Version> @@ -949,6 +950,16 @@ fgOptStartDateGmt( const char *arg ) return FG_OPTIONS_OK; } +static int +fgOptHttpd( const char * arg ) +{ + int port = atoi(arg); + if( port > 0 ) { + fgSetInt( string(flightgear::http::PROPERTY_ROOT).append("/listening-port").c_str(), port ); + } + return FG_OPTIONS_OK; +} + static int fgSetupProxy( const char *arg ) { @@ -1554,7 +1565,7 @@ struct OptionDesc { {"hud-culled", false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 }, {"atcsim", true, OPTION_CHANNEL, "", false, "dummy", 0 }, {"atlas", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 }, - {"httpd", true, OPTION_CHANNEL, "", false, "", 0 }, + {"httpd", true, OPTION_FUNC , "", false, "", fgOptHttpd }, {"jpg-httpd", true, OPTION_CHANNEL, "", false, "", 0 }, {"native", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 }, {"native-ctrls", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 }, diff --git a/src/Network/CMakeLists.txt b/src/Network/CMakeLists.txt index 56c2551f8..48036389b 100644 --- a/src/Network/CMakeLists.txt +++ b/src/Network/CMakeLists.txt @@ -10,7 +10,6 @@ set(SOURCES atlas.cxx garmin.cxx generic.cxx - httpd.cxx HTTPClient.cxx igc.cxx joyclient.cxx @@ -27,7 +26,7 @@ set(SOURCES pve.cxx ray.cxx rul.cxx - jpg-httpd.cxx + jpg-httpd.cxx ) set(HEADERS @@ -40,7 +39,6 @@ set(HEADERS atlas.hxx garmin.hxx generic.hxx - httpd.hxx HTTPClient.hxx igc.hxx joyclient.hxx @@ -57,7 +55,7 @@ set(HEADERS pve.hxx ray.hxx rul.hxx - jpg-httpd.hxx + jpg-httpd.hxx ) if(ENABLE_IAX) @@ -71,3 +69,5 @@ if(RTI_FOUND) add_subdirectory(HLA) endif() +add_subdirectory(http) + diff --git a/src/Network/http/CMakeLists.txt b/src/Network/http/CMakeLists.txt new file mode 100644 index 000000000..160307f59 --- /dev/null +++ b/src/Network/http/CMakeLists.txt @@ -0,0 +1,21 @@ +include(FlightGearComponent) + +set(SOURCES + httpd.cxx + ScreenshotUriHandler.cxx + PropertyUriHandler.cxx + JsonUriHandler.cxx + RunUriHandler.cxx + ) + +set(HEADERS + urihandler.hxx + httpd.hxx + ScreenshotUriHandler.hxx + PropertyUriHandler.hxx + JsonUriHandler.hxx + RunUriHandler.hxx + HTTPRequest.hxx + ) + +flightgear_component(Http "${SOURCES}" "${HEADERS}") diff --git a/src/Network/http/HTTPRequest.hxx b/src/Network/http/HTTPRequest.hxx new file mode 100644 index 000000000..56468d431 --- /dev/null +++ b/src/Network/http/HTTPRequest.hxx @@ -0,0 +1,65 @@ +// HTTPRequest.hxx -- Wraps a http Request +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef FG_HTTPREQUEST_HXX +#define FG_HTTPREQUEST_HXX + +#include <string> +#include <map> + +namespace flightgear { +namespace http { + +class HTTPRequest +{ +public: + HTTPRequest() {} + virtual ~HTTPRequest() {} + + std::string Method; + std::string Uri; + std::string HttpVersion; + std::string QueryString; + + std::string remoteAddress; + int remotePort; + std::string localAddress; + int localPort; + + std::string Content; + + class StringMap : public std::map<std::string,std::string> { + public: + std::string get( const std::string & key ) const { + const_iterator it = find( key ); + return it == end() ? "" : it->second; + } + }; + + StringMap RequestVariables; + + StringMap HeaderVariables; + +}; + +} +} // namespace flightgear + +#endif // FG_HTTPREQUEST_HXX diff --git a/src/Network/http/HTTPResponse.hxx b/src/Network/http/HTTPResponse.hxx new file mode 100644 index 000000000..338c7ad50 --- /dev/null +++ b/src/Network/http/HTTPResponse.hxx @@ -0,0 +1,51 @@ +// HTTPResponse.hxx -- Wraps a http Response +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef FG_HTTPRESPONSE_HXX +#define FG_HTTPRESPONSE_HXX + +#include <string> +#include <map> + +namespace flightgear { +namespace http { + +class HTTPResponse +{ +public: + HTTPResponse(); + virtual ~HTTPResponse() {} + + int StatusCode; + std::string Content; + + typedef std::map<std::string,std::string> Header_t; + Header_t Header; +}; + +inline HTTPResponse::HTTPResponse() : + StatusCode(200) +{ +} + +} +} // namespace flightgear + +#endif // FG_HTTPRESPONSE_HXX diff --git a/src/Network/http/JsonUriHandler.cxx b/src/Network/http/JsonUriHandler.cxx new file mode 100644 index 000000000..542d3d22e --- /dev/null +++ b/src/Network/http/JsonUriHandler.cxx @@ -0,0 +1,109 @@ +// JsonUriHandler.cxx -- json interface to the property tree +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +#include "JsonUriHandler.hxx" +#include <Main/fg_props.hxx> +#include <simgear/props/props.hxx> +#include <3rdparty/cjson/cJSON.h> + +using std::string; + +namespace flightgear { +namespace http { + +static const char * getPropertyTypeString( simgear::props::Type type ) +{ + switch( type ) { + case simgear::props::NONE: return "-"; + case simgear::props::ALIAS: return "alias"; + case simgear::props::BOOL: return "bool"; + case simgear::props::INT: return "int"; + case simgear::props::LONG: return "long"; + case simgear::props::FLOAT: return "float"; + case simgear::props::DOUBLE: return "double"; + case simgear::props::STRING: return "string"; + case simgear::props::UNSPECIFIED: return "unspecified"; + case simgear::props::EXTENDED: return "extended"; + case simgear::props::VEC3D: return "vec3d"; + case simgear::props::VEC4D: return "vec4d"; + default: return "?"; + } +} + +static cJSON * PropToJson( SGPropertyNode_ptr n ) +{ + cJSON * jsonProp = cJSON_CreateObject(); + cJSON_AddItemToObject(jsonProp, "path", cJSON_CreateString(n->getPath().c_str())); + cJSON_AddItemToObject(jsonProp, "name", cJSON_CreateString(n->getDisplayName().c_str())); + cJSON_AddItemToObject(jsonProp, "value", cJSON_CreateString(n->getStringValue())); + cJSON_AddItemToObject(jsonProp, "type", cJSON_CreateString(getPropertyTypeString(n->getType()))); + return jsonProp; +} + +bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse & response ) +{ + response.Header["Content-Type"] = "application/json; charset=ISO-8859-1"; + + string propertyPath = request.Uri; + + // strip the uri prefix of our handler + propertyPath = propertyPath.substr( getUri().size() ); + + // strip the querystring + size_t pos = propertyPath.find( '?' ); + if( pos != string::npos ) { + propertyPath = propertyPath.substr( 0, pos-1 ); + } + + // skip trailing '/' - not very efficient but shouldn't happen too often + while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' ) + propertyPath = propertyPath.substr(0,propertyPath.length()-1); + + SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath ); + if( false == node.valid() ) { + response.StatusCode = 400; + response.Content = "Node not found: " + propertyPath; + SG_LOG(SG_NETWORK,SG_ALERT, response.Content ); + return true; + + } + + cJSON * json = PropToJson( node ); + if( node->nChildren() > 0 ) { + cJSON * jsonArray = cJSON_CreateArray(); + for( int i = 0; i < node->nChildren(); i++ ) + cJSON_AddItemToArray( jsonArray, PropToJson( node->getChild(i) ) ); + cJSON_AddItemToObject( json, "children", jsonArray ); + } + + char * jsonString = cJSON_Print( json ); + response.Content = jsonString; + free( jsonString ); + cJSON_Delete( json ); + + + return true; + +} + +} // namespace http +} // namespace flightgear + diff --git a/src/Network/http/JsonUriHandler.hxx b/src/Network/http/JsonUriHandler.hxx new file mode 100644 index 000000000..463c5f227 --- /dev/null +++ b/src/Network/http/JsonUriHandler.hxx @@ -0,0 +1,38 @@ +// JsonUriHandler.hxx -- json interface to the property tree +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef __FG_JSON_URI_HANDLER_HXX +#define __FG_JSON_URI_HANDLER_HXX + +#include "urihandler.hxx" + +namespace flightgear { +namespace http { + +class JsonUriHandler : public URIHandler { +public: + JsonUriHandler( const char * uri = "/json/" ) : URIHandler( uri ) {} + virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response ); +}; + +} // namespace http +} // namespace flightgear + +#endif //#define __FG_SCREENSHOT_URI_HANDLER_HXX diff --git a/src/Network/http/PropertyUriHandler.cxx b/src/Network/http/PropertyUriHandler.cxx new file mode 100644 index 000000000..1319e7da2 --- /dev/null +++ b/src/Network/http/PropertyUriHandler.cxx @@ -0,0 +1,411 @@ +// PropertyUriHandler.cxx -- a web form interface to the property tree +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +#include "PropertyUriHandler.hxx" +#include <Main/fg_props.hxx> +#include <simgear/debug/logstream.hxx> +#include <simgear/misc/strutils.hxx> +#include <boost/lexical_cast.hpp> + +#include <vector> +#include <map> +#include <algorithm> +#include <cstring> + +using std::string; +using std::map; +using std::vector; + +namespace flightgear { +namespace http { + +class DOMElement { +public: + virtual ~DOMElement() {} + virtual std::string render() const = 0; +}; + +class DOMTextElement : public DOMElement { +public: + DOMTextElement( const string & text ) : _text(text) {} + virtual std::string render() const { return _text; } + +private: + string _text; +}; + + +class DOMNode : public DOMElement { +public: + DOMNode( const string & name ) : _name(name) {} + virtual ~DOMNode(); + + virtual std::string render() const; + virtual void addChild( DOMElement * child ); + virtual void setAttribute( const string & name, const string & value ); +protected: + string _name; + typedef vector<const DOMElement*> Children_t; + typedef map<string,string> Attributes_t; + Children_t _children; + Attributes_t _attributes; +}; + +DOMNode::~DOMNode() +{ + for( Children_t::const_iterator it = _children.begin(); it != _children.end(); ++it ) + delete *it; +} + +string DOMNode::render() const +{ + string reply; + reply.append( "<" ).append( _name ); + for( Attributes_t::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it ) { + reply.append( " " ); + reply.append( it->first ); + reply.append( "=\"" ); + reply.append( it->second ); + reply.append( "\"" ); + } + + if( _children.empty() ) { + reply.append( " />\r" ); + return reply; + } else { + reply.append( ">" ); + } + + for( Children_t::const_iterator it = _children.begin(); it != _children.end(); ++it ) { + reply.append( (*it)->render() ); + } + + reply.append( "</" ).append( _name ).append( ">\r" ); + + return reply; +} + +void DOMNode::addChild( DOMElement * child ) +{ + _children.push_back( child ); +} + +void DOMNode::setAttribute( const string & name, const string & value ) +{ + _attributes[name] = value; +} + +class SortedChilds : public simgear::PropertyList { +public: + SortedChilds( SGPropertyNode_ptr node ) { + for (int i = 0; i < node->nChildren(); i++) + push_back(node->getChild(i)); + std::sort(begin(), end(), CompareNodes()); + } +private: + class CompareNodes { + public: + bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const { + int r = strcmp(a->getName(), b->getName()); + return r ? r < 0 : a->getIndex() < b->getIndex(); + } + }; +}; + +static const char * getPropertyTypeString( simgear::props::Type type ) +{ + switch( type ) { + case simgear::props::NONE: return ""; + case simgear::props::ALIAS: return "alias"; + case simgear::props::BOOL: return "bool"; + case simgear::props::INT: return "int"; + case simgear::props::LONG: return "long"; + case simgear::props::FLOAT: return "float"; + case simgear::props::DOUBLE: return "double"; + case simgear::props::STRING: return "string"; + case simgear::props::UNSPECIFIED: return "unspecified"; + case simgear::props::EXTENDED: return "extended"; + case simgear::props::VEC3D: return "vec3d"; + case simgear::props::VEC4D: return "vec4d"; + default: return "?"; + } +} + +DOMElement * createHeader( const string & prefix, const string & propertyPath ) +{ + using namespace simgear::strutils; + + string path = prefix; + + DOMNode * root = new DOMNode( "div" ); + root->setAttribute( "id", "breadcrumb" ); + + DOMNode * headline = new DOMNode( "h3" ); + root->addChild( headline ); + headline->addChild( new DOMTextElement("FlightGear Property Browser") ); + + DOMNode * breadcrumb = new DOMNode("ul"); + root->addChild( breadcrumb ); + + DOMNode * li = new DOMNode("li"); + breadcrumb->addChild( li ); + DOMNode * a = new DOMNode("a"); + li->addChild( a ); + a->setAttribute( "href", path ); + a->addChild( new DOMTextElement( "[root]" ) ); + + string_list items = split( propertyPath, "/" ); + for( string_list::iterator it = items.begin(); it != items.end(); ++it ) { + if( (*it).empty() ) continue; + path.append( *it ).append( "/" ); + + li = new DOMNode("li"); + breadcrumb->addChild( li ); + a = new DOMNode("a"); + li->addChild( a ); + a->setAttribute( "href", path ); + a->addChild( new DOMTextElement( (*it) ) ); + } + + return root; +} + +static DOMElement * renderPropertyValueElement( SGPropertyNode_ptr node ) +{ + string value = node->getStringValue(); + int len = value.length(); + + if( len < 15 ) len = 15; + + DOMNode * root; + + if( len < 60 ) { + root = new DOMNode( "input" ); + root->setAttribute( "type", "text" ); + root->setAttribute( "name", node->getDisplayName() ); + root->setAttribute( "value", value ); + root->setAttribute( "size", boost::lexical_cast<std::string>( len ) ); + root->setAttribute( "maxlength", "2047" ); + } else { + int rows = (len / 60)+1; + int cols = 60; + root = new DOMNode( "textarea" ); + root->setAttribute( "name", node->getDisplayName() ); + root->setAttribute( "cols", boost::lexical_cast<std::string>( cols ) ); + root->setAttribute( "rows", boost::lexical_cast<std::string>( rows ) ); + root->setAttribute( "maxlength", "2047" ); + root->addChild( new DOMTextElement( value ) ); + } + + return root; +} + +bool PropertyUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse & response ) +{ + + string propertyPath = request.Uri; + + // strip the uri prefix of our handler + propertyPath = propertyPath.substr( getUri().size() ); + + // strip the querystring + size_t pos = propertyPath.find( '?' ); + if( pos != string::npos ) { + propertyPath = propertyPath.substr( 0, pos-1 ); + } + + // skip trailing '/' - not very efficient but shouldn't happen too often + while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' ) + propertyPath = propertyPath.substr(0,propertyPath.length()-1); + + if( request.RequestVariables.get("submit") == "update" ) { + // update leaf + string value = request.RequestVariables.get("value"); + SG_LOG(SG_NETWORK,SG_INFO, "httpd: setting " << propertyPath << " to '" << value << "'" ); + fgSetString( propertyPath.c_str(), value ); + } + + if( request.RequestVariables.get("submit") == "set" ) { + for( HTTPRequest::StringMap::const_iterator it = request.RequestVariables.begin(); it != request.RequestVariables.end(); ++it ) { + if( it->first == "submit" ) continue; + string pp = propertyPath + "/" + it->first; + SG_LOG(SG_NETWORK,SG_INFO, "httpd: setting " << pp << " to '" << it->second << "'" ); + fgSetString( pp, it->second ); + } + } + + // build the response + DOMNode * html = new DOMNode( "html" ); + html->setAttribute( "lang", "en" ); + + DOMNode * head = new DOMNode( "head" ); + html->addChild( head ); + + DOMNode * e; + e = new DOMNode( "title" ); + head->addChild( e ); + e->addChild( new DOMTextElement( string("FlightGear Property Browser - ") + propertyPath ) ); + + e = new DOMNode( "link" ); + head->addChild( e ); + e->setAttribute( "href", "/css/props.css" ); + e->setAttribute( "rel", "stylesheet" ); + e->setAttribute( "type", "text/css" ); + + DOMNode * body = new DOMNode( "body" ); + html->addChild( body ); + + SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath ); + if( false == node.valid() ) { + DOMNode * headline = new DOMNode( "h3" ); + body->addChild( headline ); + headline->addChild( new DOMTextElement( "Non-existent node requested!" ) ); + e = new DOMNode( "b" ); + e->addChild( new DOMTextElement( propertyPath ) ); + // does not exist + body->addChild( e ); + + } else if( node->nChildren() > 0 ) { + // Render the list of children + body->addChild( createHeader( getUri(), propertyPath )); + + DOMNode * table = new DOMNode("table"); + body->addChild( table ); + + DOMNode * tr = new DOMNode( "tr" ); + table->addChild( tr ); + + DOMNode * th = new DOMNode( "th" ); + tr->addChild( th ); + th->addChild( new DOMTextElement( " " ) ); + + th = new DOMNode( "th" ); + tr->addChild( th ); + th->addChild( new DOMTextElement( "Property" ) ); + th->setAttribute( "id", "property" ); + + th = new DOMNode( "th" ); + tr->addChild( th ); + th->addChild( new DOMTextElement( "Value" ) ); + th->setAttribute( "id", "value" ); + + th = new DOMNode( "th" ); + tr->addChild( th ); + th->addChild( new DOMTextElement( "Type" ) ); + th->setAttribute( "id", "type" ); + + SortedChilds sortedChilds( node ); + for(SortedChilds::iterator it = sortedChilds.begin(); it != sortedChilds.end(); ++it ) { + tr = new DOMNode( "tr" ); + table->addChild( tr ); + + SGPropertyNode_ptr child = *it; + string name = child->getDisplayName(true); + + DOMNode * td; + + // Expand Link + td = new DOMNode("td"); + tr->addChild( td ); + td->setAttribute( "id", "expand" ); + if ( child->nChildren() > 0 ) { + DOMNode * a = new DOMNode("a"); + td->addChild( a ); + a->setAttribute( "href", getUri() + propertyPath + "/" + name ); + a->addChild( new DOMTextElement( "(+)" )); + } + + // Property Name + td = new DOMNode("td"); + tr->addChild( td ); + td->setAttribute( "id", "property" ); + DOMNode * a = new DOMNode("a"); + td->addChild( a ); + a->setAttribute( "href", getUri() + propertyPath + "/" + name ); + a->addChild( new DOMTextElement( name ) ); + + // Value + td = new DOMNode("td"); + tr->addChild( td ); + td->setAttribute( "id", "value" ); + if ( child->nChildren() == 0 ) { + DOMNode * form = new DOMNode("form"); + td->addChild( form ); + form->setAttribute( "method", "GET" ); + form->setAttribute( "action", getUri() + propertyPath ); + + e = new DOMNode( "input" ); + form->addChild( e ); + e->setAttribute( "type", "submit" ); + e->setAttribute( "value", "set" ); + e->setAttribute( "name", "submit" ); + + form->addChild( renderPropertyValueElement( node->getNode( name ) ) ); + + } else { + td->addChild( new DOMTextElement( " " ) ); + } + + // Property Type + td = new DOMNode("td"); + tr->addChild( td ); + td->setAttribute( "id", "type" ); + td->addChild( + new DOMTextElement( getPropertyTypeString(node->getNode( name )->getType()) ) ); + + } + } else { + // Render a single property + body->addChild( createHeader( getUri(), propertyPath )); + e = new DOMNode( "div" ); + body->addChild( e ); + + e->setAttribute( "id", "currentvalue" ); + e->addChild( new DOMTextElement( "Current Value: " ) ); + e->addChild( new DOMTextElement( node->getStringValue() ) ); + + DOMNode * form = new DOMNode("form"); + body->addChild( form ); + form->setAttribute( "method", "GET" ); + form->setAttribute( "action", getUri() + propertyPath ); + + e = new DOMNode( "input" ); + form->addChild( e ); + e->setAttribute( "type", "submit" ); + e->setAttribute( "value", "update" ); + e->setAttribute( "name", "submit" ); + + form->addChild( renderPropertyValueElement( node ) ); + } + + // Send the response + response.Content = "<!DOCTYPE html>"; + response.Content.append( html->render() ); + delete html; + response.Header["Content-Type"] = "text/html; charset=ISO-8859-1"; + + return true; + +} + +} // namespace http +} // namespace flightgear + diff --git a/src/Network/http/PropertyUriHandler.hxx b/src/Network/http/PropertyUriHandler.hxx new file mode 100644 index 000000000..4557552f5 --- /dev/null +++ b/src/Network/http/PropertyUriHandler.hxx @@ -0,0 +1,38 @@ +// PropertyUriHandler.cxx -- a web form interface to the property tree +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef __FG_PROPERTY_URI_HANDLER_HXX +#define __FG_PROPERTY_URI_HANDLER_HXX + +#include "urihandler.hxx" + +namespace flightgear { +namespace http { + +class PropertyUriHandler : public URIHandler { +public: + PropertyUriHandler( const char * uri = "/prop/" ) : URIHandler( uri ) {} + virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response ); +}; + +} // namespace http +} // namespace flightgear + +#endif //#define __FG_PROPERTY_URI_HANDLER_HXX diff --git a/src/Network/http/RunUriHandler.cxx b/src/Network/http/RunUriHandler.cxx new file mode 100644 index 000000000..798a81874 --- /dev/null +++ b/src/Network/http/RunUriHandler.cxx @@ -0,0 +1,55 @@ +// RunUriHandler.cxx -- Run a flightgear command +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +#include "RunUriHandler.hxx" +#include <simgear/props/props.hxx> +#include <simgear/structure/commands.hxx> +#include <Main/globals.hxx> + +using std::string; + +namespace flightgear { +namespace http { + +bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response ) +{ + response.Header["Content-Type"] = "text/plain"; + string command = request.RequestVariables.get("value"); + if( command.empty() ) { + response.StatusCode = 400; + response.Content = "command not specified"; + return true; + } + + SGPropertyNode args; + if ( globals->get_commands()->execute(command.c_str(), &args) ) { + response.Content = "ok."; + return true; + } + + response.Content = "command '" + command + "' failed."; + SG_LOG( SG_NETWORK, SG_ALERT, response.Content ); + return true; +} + +} // namespace http +} // namespace flightgear + diff --git a/src/Network/http/RunUriHandler.hxx b/src/Network/http/RunUriHandler.hxx new file mode 100644 index 000000000..0d7b0ecfe --- /dev/null +++ b/src/Network/http/RunUriHandler.hxx @@ -0,0 +1,38 @@ +// RunUriHandler.hxx -- Provide screenshots via http +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef __FG_RUN_URI_HANDLER_HXX +#define __FG_RUN_URI_HANDLER_HXX + +#include "urihandler.hxx" + +namespace flightgear { +namespace http { + +class RunUriHandler : public URIHandler { +public: + RunUriHandler( const char * uri = "/run.cgi" ) : URIHandler( uri ) {} + virtual bool handleRequest( const HTTPRequest & request, HTTPResponse & response ); +}; + +} // namespace http +} // namespace flightgear + +#endif //#define __FG_RUN_URI_HANDLER_HXX diff --git a/src/Network/http/ScreenshotUriHandler.cxx b/src/Network/http/ScreenshotUriHandler.cxx new file mode 100644 index 000000000..42204e136 --- /dev/null +++ b/src/Network/http/ScreenshotUriHandler.cxx @@ -0,0 +1,39 @@ +// ScreenshotUriHandler.cxx -- Provide screenshots via http +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +#include "ScreenshotUriHandler.hxx" + +using std::string; + +namespace flightgear { +namespace http { + +bool ScreenshotUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response ) +{ + response.Header["Content-Type"] = "image/jpeg"; +// string response = "Screenshot - not yet implemented :-("; +// mg_send_data( connection, response.c_str(), response.length() ); + return true; +} + +} // namespace http +} // namespace flightgear + diff --git a/src/Network/http/ScreenshotUriHandler.hxx b/src/Network/http/ScreenshotUriHandler.hxx new file mode 100644 index 000000000..954dbb306 --- /dev/null +++ b/src/Network/http/ScreenshotUriHandler.hxx @@ -0,0 +1,38 @@ +// ScreenshotUriHandler.hxx -- Provide screenshots via http +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef __FG_SCREENSHOT_URI_HANDLER_HXX +#define __FG_SCREENSHOT_URI_HANDLER_HXX + +#include "urihandler.hxx" + +namespace flightgear { +namespace http { + +class ScreenshotUriHandler : public URIHandler { +public: + ScreenshotUriHandler( const char * uri = "/screenshot/" ) : URIHandler( uri ) {} + virtual bool handleRequest( const HTTPRequest & request, HTTPResponse & response ); +}; + +} // namespace http +} // namespace flightgear + +#endif //#define __FG_SCREENSHOT_URI_HANDLER_HXX diff --git a/src/Network/http/httpd.cxx b/src/Network/http/httpd.cxx new file mode 100644 index 000000000..9fd2f31f1 --- /dev/null +++ b/src/Network/http/httpd.cxx @@ -0,0 +1,244 @@ +// httpd.cxx -- a http daemon subsystem based on Mongoose http +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#include "httpd.hxx" +#include "HTTPRequest.hxx" +#include "ScreenshotUriHandler.hxx" +#include "PropertyUriHandler.hxx" +#include "JsonUriHandler.hxx" +#include "RunUriHandler.hxx" +#include <Main/fg_props.hxx> +#include <Include/version.h> +#include <3rdparty/mongoose/mongoose.h> + +#include <string> +#include <vector> + +using std::string; +using std::vector; + +namespace flightgear { +namespace http { + +const char * PROPERTY_ROOT = "/sim/http"; + +class MongooseHttpd : public FGHttpd { +public: + MongooseHttpd( SGPropertyNode_ptr ); + ~MongooseHttpd(); + void init(); + void bind(); + void unbind(); + void shutdown(); + void update( double dt ); + +private: + int requestHandler( struct mg_connection * ); + static int staticRequestHandler( struct mg_connection * ); + + struct mg_server *_server; + SGPropertyNode_ptr _configNode; + + typedef int (MongooseHttpd::*handler_t)( struct mg_connection * ); + typedef vector<SGSharedPtr<URIHandler> > URIHandlerMap; + URIHandlerMap _uriHandlers; +}; + +MongooseHttpd::MongooseHttpd( SGPropertyNode_ptr configNode ) : + _server(NULL), + _configNode(configNode) +{ +} + +MongooseHttpd::~MongooseHttpd() +{ + mg_destroy_server(&_server); +} + +void MongooseHttpd::init() +{ + SGPropertyNode_ptr n = _configNode->getNode("uri-handler"); + if( n.valid() ) { + const char * uri; + + if( (uri=n->getStringValue( "screenshot" ))[0] != 0 ) { + SG_LOG(SG_NETWORK,SG_INFO,"httpd: adding screenshot uri handler at " << uri ); + _uriHandlers.push_back( new flightgear::http::ScreenshotUriHandler( uri ) ); + } + + if( (uri=n->getStringValue( "property" ))[0] != 0 ) { + SG_LOG(SG_NETWORK,SG_INFO,"httpd: adding screenshot property handler at " << uri ); + _uriHandlers.push_back( new flightgear::http::PropertyUriHandler( uri ) ); + } + + if( (uri=n->getStringValue( "json" ))[0] != 0 ) { + SG_LOG(SG_NETWORK,SG_INFO,"httpd: adding json property handler at " << uri ); + _uriHandlers.push_back( new flightgear::http::JsonUriHandler( uri ) ); + } + + if( (uri=n->getStringValue( "run" ))[0] != 0 ) { + SG_LOG(SG_NETWORK,SG_INFO,"httpd: adding run handler at " << uri ); + _uriHandlers.push_back( new flightgear::http::RunUriHandler( uri ) ); + } + } + + _server = mg_create_server(this); + + n = _configNode->getNode("options"); + if( n.valid() ) { + + string docRoot = n->getStringValue( "document-root", fgGetString("/sim/fg-root") ); + if( docRoot[0] != '/' ) docRoot.insert(0,"/").insert(0,fgGetString("/sim/fg-root")); + + mg_set_option(_server, "document_root", docRoot.c_str() ); + + mg_set_option(_server, "listening_port", n->getStringValue( "listening-port", "8080" ) ); +// mg_set_option(_server, "url_rewrites", n->getStringValue( "url-rewrites", "" ) ); + mg_set_option(_server, "enable_directory_listing", n->getStringValue( "enable-directory-listing", "yes" ) ); + mg_set_option(_server, "idle_timeout_ms", n->getStringValue( "idle-timeout-ms", "30000" ) ); + mg_set_option(_server, "index_files", n->getStringValue( "index-files", "index.html" ) ); + } + + mg_set_request_handler( _server, MongooseHttpd::staticRequestHandler ); +} + +void MongooseHttpd::bind() +{ +} + +void MongooseHttpd::unbind() +{ + mg_destroy_server(&_server); + _uriHandlers.clear(); +} + +void MongooseHttpd::shutdown() +{ +} + +void MongooseHttpd::update( double dt ) +{ + mg_poll_server( _server, 0 ); +} + + +class MongooseHTTPRequest : public HTTPRequest { +private: + inline string NotNull( const char * cp ) { return NULL == cp ? "" : cp; } +public: + MongooseHTTPRequest( struct mg_connection * connection ) { + Method = NotNull(connection->request_method); + Uri = urlDecode( NotNull(connection->uri)); + HttpVersion = NotNull(connection->http_version); + QueryString = urlDecode(NotNull(connection->query_string)); + + remoteAddress = NotNull(connection->remote_ip); + remotePort = connection->remote_port; + localAddress = NotNull(connection->local_ip); + localPort = connection->local_port; + + using namespace simgear::strutils; + string_list pairs = split(string(QueryString), "&"); + for( string_list::iterator it = pairs.begin(); it != pairs.end(); ++it ) { + string_list nvp = split( *it, "=" ); + if( nvp.size() != 2 ) continue; + RequestVariables.insert( make_pair( nvp[0], nvp[1] ) ); + } + + for( int i = 0; i < connection->num_headers; i++ ) + HeaderVariables[connection->http_headers[i].name] = connection->http_headers[i].value; + + Content = NotNull(connection->content); + + } + + string urlDecode( const string & s ) { + string r = ""; + int max = s.length(); + int a, b; + for ( int i = 0; i < max; i++ ) { + if ( s[i] == '+' ) { + r += ' '; + } else if ( s[i] == '%' && i + 2 < max + && isxdigit(s[i + 1]) + && isxdigit(s[i + 2]) ) { + i++; + a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10; + i++; + b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10; + r += (char)(a * 16 + b); + } else { + r += s[i]; + } + } + return r; + } +}; + +int MongooseHttpd::requestHandler( struct mg_connection * connection ) +{ + MongooseHTTPRequest request(connection); + HTTPResponse response; + response.Header["Server"] = "FlightGear/" FLIGHTGEAR_VERSION " Mongoose/" MONGOOSE_VERSION; + response.Header["Connection"] = "close"; + response.Header["Cache-Control"] = "no-cache"; + { + char buf[64]; + time_t now = time(NULL); + strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now)); + response.Header["Date"] = buf; + } + + bool processed = false; + for( URIHandlerMap::iterator it = _uriHandlers.begin(); it != _uriHandlers.end(); ++it ) { + URIHandler * handler = *it; + const string uri = connection->uri; + if( uri.find( handler->getUri() ) == 0 ) { + processed = handler->handleRequest( request, response ); + break; + } + } + + if( processed ) { + for( HTTPResponse::Header_t::const_iterator it = response.Header.begin(); it != response.Header.end(); ++it ) { + const string name = it->first; + const string value = it->second; + if( name.empty() || value.empty() ) continue; + mg_send_header( connection, name.c_str(), value.c_str() ); + } + mg_send_status( connection, response.StatusCode ); + mg_send_data( connection, response.Content.c_str(), response.Content.length() ); + } + + return processed ? MG_REQUEST_PROCESSED : MG_REQUEST_NOT_PROCESSED; +} + +int MongooseHttpd::staticRequestHandler( struct mg_connection * connection ) +{ + return static_cast<MongooseHttpd*>(connection->server_param)->requestHandler( connection ); +} + +FGHttpd * FGHttpd::createInstance( SGPropertyNode_ptr configNode ) +{ + return configNode.valid() ? new MongooseHttpd( configNode ) : NULL; +} + +} // namespace http +} // namespace flightgear diff --git a/src/Network/httpd.hxx b/src/Network/http/httpd.hxx similarity index 57% rename from src/Network/httpd.hxx rename to src/Network/http/httpd.hxx index 986713cbc..764785ced 100644 --- a/src/Network/httpd.hxx +++ b/src/Network/http/httpd.hxx @@ -1,9 +1,8 @@ -// httpd.hxx -- FGFS http property manager interface / external script -// and control class +// httpd.hxx -- a http daemon subsystem based on Mongoose http // -// Written by Curtis Olson, started June 2001. +// Written by Torsten Dreyer, started April 2014. // -// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt +// Copyright (C) 2014 Torsten Dreyer // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -18,33 +17,28 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -// -// $Id$ + +#ifndef FG_HTTPD_HXX +#define FG_HTTPD_HXX + +#include <simgear/structure/subsystem_mgr.hxx> +#include <simgear/props/props.hxx> -#ifndef _FG_HTTPD_HXX -#define _FG_HTTPD_HXX +namespace flightgear { +namespace http { -#ifdef HAVE_CONFIG_H -# include <config.h> -#endif +extern const char * PROPERTY_ROOT; -#include "protocol.hxx" - -class HttpdServer; - -class FGHttpd : public FGProtocol +class FGHttpd : public SGSubsystem { - int port; - HttpdServer *server; - public: - FGHttpd( int p ); - ~FGHttpd(); - - bool open(); - bool process(); - bool close(); + static FGHttpd * createInstance( SGPropertyNode_ptr configNode ); }; -#endif // _FG_HTTPD_HXX +} +} // namespace flightgear + +#endif // FG_HTTPD_HXX + + diff --git a/src/Network/http/urihandler.hxx b/src/Network/http/urihandler.hxx new file mode 100644 index 000000000..ec6d5611d --- /dev/null +++ b/src/Network/http/urihandler.hxx @@ -0,0 +1,61 @@ +// urihandler.hxx -- a base class for URI handler +// +// Written by Torsten Dreyer, started April 2014. +// +// Copyright (C) 2014 Torsten Dreyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef __FG_URI_HANDLER_HXX +#define __FG_URI_HANDLER_HXX + +#include "HTTPRequest.hxx" +#include "HTTPResponse.hxx" +#include <simgear/structure/SGReferenced.hxx> +#include <string> +#include <map> + +namespace flightgear { +namespace http { + +class URIHandler : public SGReferenced { +public: + URIHandler( const char * uri ) : _uri(uri) {} + virtual ~URIHandler() {} + + virtual bool handleRequest( const HTTPRequest & request, HTTPResponse & response ) { + if( request.Method == "GET" ) return handleGetRequest( request, response ); + if( request.Method == "PUT" ) return handlePutRequest( request, response ); + return false; + } + + virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response ) { + return false; + } + + virtual bool handlePutRequest( const HTTPRequest & request, HTTPResponse & response ) { + return false; + } + + const std::string & getUri() const { return _uri; } + +private: + std::string _uri; +}; + +} // namespace http +} // namespace flightgear + +#endif //#define __FG_URI_HANDLER_HXX diff --git a/src/Network/httpd.cxx b/src/Network/httpd.cxx deleted file mode 100644 index 880e3ebf4..000000000 --- a/src/Network/httpd.cxx +++ /dev/null @@ -1,445 +0,0 @@ -// httpd.cxx -- FGFS http property manager interface / external script -// and control class -// -// Written by Curtis Olson, started June 2001. -// -// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -// -// $Id$ - - -#ifdef HAVE_CONFIG_H -# include <config.h> -#endif - -#include <simgear/compiler.h> - -#include <algorithm> // sort() -#include <cstdlib> // atoi() atof() -#include <cstring> -#include <cstdio> -#include <string> - -#include <simgear/debug/logstream.hxx> -#include <simgear/io/sg_netChat.hxx> -#include <simgear/io/iochannel.hxx> -#include <simgear/math/sg_types.hxx> -#include <simgear/structure/commands.hxx> -#include <simgear/props/props.hxx> -#include <simgear/misc/strutils.hxx> - -#include <Main/fg_props.hxx> -#include <Main/globals.hxx> - -#include "httpd.hxx" - -using std::string; - -/* simple httpd server that makes an hasty stab at following the http - 1.1 rfc. */ - -////////////////////////////////////////////////////////////// -// class HttpdChannel -////////////////////////////////////////////////////////////// - -class HttpdChannel : public simgear::NetChat -{ - simgear::NetBuffer buffer; - - string urlEncode(string); - string urlDecode(string); - -public: - - HttpdChannel() : buffer(512) { setTerminator("\r\n"); } - - virtual void collectIncomingData (const char* s, int n) { - buffer.append(s,n); - } - - // Handle the actual http request - virtual void foundTerminator(void); -}; - - -////////////////////////////////////////////////////////////// -// class HttpdServer -////////////////////////////////////////////////////////////// - -class HttpdServer : private simgear::NetChannel -{ - virtual bool writable (void) { return false; } - - virtual void handleAccept (void) { - simgear::IPAddress addr; - int handle = accept ( &addr ); - SG_LOG( SG_IO, SG_INFO, "Client " << addr.getHost() << ":" << addr.getPort() << " connected" ); - - HttpdChannel *hc = new HttpdChannel; - hc->setHandle ( handle ); - poller.addChannel( hc ); - } - - simgear::NetChannelPoller poller; -public: - - HttpdServer ( int port ); - - void poll() - { - poller.poll(); - } -}; - -HttpdServer::HttpdServer(int port) -{ - if (!open()) - { - SG_LOG( SG_IO, SG_ALERT, "Failed to open HTTP port."); - return; - } - - if (0 != bind( "", port )) - { - SG_LOG( SG_IO, SG_ALERT, "Failed to bind HTTP port."); - return; - } - - if (0 != listen( 5 )) - { - SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HTTP port."); - return; - } - - poller.addChannel(this); - - SG_LOG(SG_IO, SG_ALERT, "Httpd server started on port " << port); -} - -////////////////////////////////////////////////////////////// -// class FGHttpd -////////////////////////////////////////////////////////////// - -FGHttpd::FGHttpd(int p) : - port(p), - server(NULL) -{ -} - -FGHttpd::~FGHttpd() -{ - delete server; -} - -bool FGHttpd::open() { - if ( is_enabled() ) { - SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " - << "is already in use, ignoring" ); - return false; - } - - server = new HttpdServer( port ); - - set_hz( 15 ); // default to processing requests @ 15Hz - set_enabled( true ); - - return true; -} - - -bool FGHttpd::process() { - server->poll(); - return true; -} - - -bool FGHttpd::close() { - if (!server) - return true; - - SG_LOG( SG_IO, SG_INFO, "closing FGHttpd" ); - - delete server; - server = NULL; - set_enabled( false ); - - return true; -} - - -class CompareNodes { -public: - bool operator() (const SGPropertyNode *a, const SGPropertyNode *b) const { - int r = strcmp(a->getName(), b->getName()); - return r ? r < 0 : a->getIndex() < b->getIndex(); - } -}; - -static string makeLinksForPath( const string & request ) -{ - using namespace simgear::strutils; - string reply = "<A HREF=\"/\">/</A>"; - string path = "/"; - - string_list sl = split( request, "/" ); - for( string_list::iterator it = sl.begin(); it != sl.end(); ++it ) { - if( (*it).empty() ) continue; - path.append( *it ).append( "/" ); - reply.append("<A HREF=\"") - .append(path) - .append("\">") - .append(*it) - .append("</A>/"); - - } - return reply; -} - -// Handle http GET requests -void HttpdChannel::foundTerminator (void) { - - closeWhenDone (); - - const string s = buffer.getData(); - - if ( s.find( "GET " ) == 0 ) { - SG_LOG( SG_IO, SG_INFO, "echo: " << s ); - - string rest = s.substr(4); - string request; - string tmp; - - string::size_type pos = rest.find( " " ); - if ( pos != string::npos ) { - request = rest.substr( 0, pos ); - } else { - request = "/"; - } - - SGPropertyNode *node = NULL; - pos = request.find( "?" ); - if ( pos != string::npos ) { - // request to update property value - string args = request.substr( pos + 1 ); - request = request.substr( 0, pos ); - SG_LOG( SG_IO, SG_INFO, "'" << request << "' '" << args << "'" ); - request = urlDecode(request); - - // parse args looking for "value=" - bool done = false; - while ( ! done ) { - string arg; - pos = args.find("&"); - if ( pos != string::npos ) { - arg = args.substr( 0, pos ); - args = args.substr( pos + 1 ); - } else { - arg = args; - done = true; - } - - SG_LOG( SG_IO, SG_INFO, " arg = " << arg ); - string::size_type apos = arg.find("="); - if ( apos != string::npos ) { - string a = arg.substr( 0, apos ); - string b = arg.substr( apos + 1 ); - SG_LOG( SG_IO, SG_INFO, " a = " << a << " b = " << b ); - if ( request == "/run.cgi" ) { - // execute a command - if ( a == "value" ) { - SGPropertyNode args; - if ( !globals->get_commands() - ->execute(urlDecode(b).c_str(), &args) ) - { - SG_LOG( SG_NETWORK, SG_ALERT, - "Command " << urlDecode(b) - << " failed."); - } - - } - } else { - if ( a == "value" ) { - // update a property value - fgSetString( request.c_str(), - urlDecode(b).c_str() ); - } - } - } - } - } else { - request = urlDecode(request); - } - - node = globals->get_props()->getNode(request.c_str()); - - string response = ""; - response += "<HTML LANG=\"en\">"; - response += getTerminator(); - - response += "<HEAD>"; - response += getTerminator(); - - response += "<TITLE>FlightGear - "; - response += request; - response += "</TITLE>"; - response += getTerminator(); - - response += "</HEAD>"; - response += getTerminator(); - - response += "<BODY>"; - response += getTerminator(); - - if (node == NULL) { - response += "<H3>Non-existent node requested!</H3>"; - response += getTerminator(); - - response += "<B>"; - response += request.c_str(); - response += "</B> does not exist."; - response += getTerminator(); - } else if ( node->nChildren() > 0 ) { - // request is a path with children - response += "<H3>Contents of \""; - response += makeLinksForPath( request ); - response += "\"</H3>"; - response += getTerminator(); - - - vector<SGPropertyNode *> children; - for (int i = 0; i < node->nChildren(); i++) - children.push_back(node->getChild(i)); - std::sort(children.begin(), children.end(), CompareNodes()); - - vector<SGPropertyNode *>::iterator it, end = children.end(); - for (it = children.begin(); it != end; ++it) { - SGPropertyNode *child = *it; - string name = child->getDisplayName(true); - string line = ""; - if ( child->nChildren() > 0 ) { - line += "<B><A HREF=\""; - line += request; - if ( request.substr(request.length() - 1, 1) != "/" ) { - line += "/"; - } - line += urlEncode(name); - line += "\">"; - line += name; - line += "</A></B>"; - line += "/<BR>"; - } else { - string value = node->getStringValue ( name.c_str(), "" ); - line += "<B>"; - line += name; - line += "</B> <A HREF=\""; - line += request; - if ( request.substr(request.length() - 1, 1) != "/" ) { - line += "/"; - } - line += urlEncode(name); - line += "\">("; - line += value; - line += ")</A><BR>"; - } - response += line; - response += getTerminator(); - } - } else { - // request for an individual data member - string value = node->getStringValue(); - - response += "<form method=\"GET\" action=\""; - response += urlEncode(request); - response += "\">"; - response += "<B>"; - response += makeLinksForPath(request); - response += "</B> = "; - response += "<input type=text name=value size=\"15\" value=\""; - response += value; - response += "\" maxlength=2047>"; - response += "<input type=submit value=\"update\" name=\"submit\">"; - response += "</FORM>"; - } - response += "</BODY>"; - response += getTerminator(); - - response += "</HTML>"; - response += getTerminator(); - - push( "HTTP/1.1 200 OK" ); - push( getTerminator() ); - - SG_LOG( SG_IO, SG_INFO, "size = " << response.length() ); - char ctmp[256]; - sprintf(ctmp, "Content-Length: %u", (unsigned)response.length()); - push( ctmp ); - push( getTerminator() ); - - push( "Connection: close" ); - push( getTerminator() ); - - push( "Content-Type: text/html" ); - push( getTerminator() ); - push( getTerminator() ); - - push( response.c_str() ); - } - - buffer.remove(); -} - - -// encode everything but "a-zA-Z0-9_.-/" (see RFC2396) -string HttpdChannel::urlEncode(string s) { - string r = ""; - - for ( int i = 0; i < (int)s.length(); i++ ) { - if ( isalnum(s[i]) || s[i] == '_' || s[i] == '.' - || s[i] == '-' || s[i] == '/' ) { - r += s[i]; - } else { - char buf[16]; - sprintf(buf, "%%%02X", (unsigned char)s[i]); - r += buf; - } - } - return r; -} - - -string HttpdChannel::urlDecode(string s) { - string r = ""; - int max = s.length(); - int a, b; - - for ( int i = 0; i < max; i++ ) { - if ( s[i] == '+' ) { - r += ' '; - } else if ( s[i] == '%' && i + 2 < max - && isxdigit(s[i + 1]) - && isxdigit(s[i + 2]) ) { - i++; - a = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10; - i++; - b = isdigit(s[i]) ? s[i] - '0' : toupper(s[i]) - 'A' + 10; - r += (char)(a * 16 + b); - } else { - r += s[i]; - } - } - return r; -}