177 #define MHD_PLATFORM_H WIN32
178 #include <sys/types.h>
182 #include <ws2tcpip.h>
183 #define ssize_t size_t
184 #define off_t ptrdiff_t
185 #include <sys/stat.h>
194 #define SSIZE_MAX 100000000L
202 #define scene_path "http://dug9.users.sourceforge.net/web3d/townsite_2014/townsite.x3d" //"C:/Users/Public/dev/source2/placeholders/townsite_2014/townsite.x3d"
203 #include <cdllFreeWRL.h>
205 #include "../lib/SSRhelper.h"
206 #include "cson/fw_cson.h"
209 int runFW(
char *url){
211 fwctx = dllFreeWRL_dllFreeWRL();
212 dllFreeWRL_commandline(fwctx,
"set_keyval,SSR,true");
214 dllFreeWRL_onInit(fwctx,640,480,NULL,FALSE,FALSE);
215 dllFreeWRL_onLoad(fwctx,url);
216 dllFreeWRL_commandline(fwctx,
"pin,FF");
222 dllFreeWRL_onClose(fwctx);
235 int (*fkey)(
const char *
key,
int index,
cson_value *val,
void *cbdata);
236 int (*fval)(
cson_value *val,
int index,
void *cbdata);
243 int walk_array_cson(
cson_array *arr,
void *cbdata);
250 int rc = cson_object_iter_init( obj, &iter );
252 printf(
"error, but can only fail if obj is NULL\n");
256 while( (kvp = cson_object_iter_next(&iter)) )
260 rc = wcbd->fkey(cson_string_cstr(ckey),i,v,cbdata);
262 rc = wcbd->fval(v,0,cbdata);
268 int walk_array_cson(
cson_array *arr,
void *cbdata){
271 len = cson_array_length_get(arr);
273 for( i = 0; i < len; ++i ) {
275 vi = cson_array_get( arr, i );
276 rc = wcbd->fval(vi,i,cbdata);
281 int cb_print_key(
const char *
key,
int index,
cson_value *val,
void *cbdata){
284 indent = *((
int*)(wcbd->data));
285 if(index) printf(
",");
286 printf(
"\"%s\":", key );
289 int cb_print_val(
cson_value *val,
int index,
void *cbdata){
294 indent = *((
int*)wcbd->data);
296 if(index) printf(
",");
297 switch(cson_value_type_id(val))
299 case CSON_TYPE_UNDEF:
305 if(cson_value_get_bool(val))
311 case CSON_TYPE_INTEGER:
314 rc = cson_value_fetch_integer(val, &ii );
318 case CSON_TYPE_DOUBLE:
321 rc = cson_value_fetch_double(val, &dd );
325 case CSON_TYPE_STRING:
328 rc = cson_value_fetch_string(val, &str );
329 printf(
"\"%s\"",cson_string_cstr(str));
332 case CSON_TYPE_OBJECT:
336 rc = walk_obj_cson(obji,cbdata);
340 case CSON_TYPE_ARRAY:
343 rc = cson_value_fetch_array(val,&ar);
346 rc = walk_array_cson(ar,cbdata);
359 cbdata.data = &indent;
360 cbdata.fkey = cb_print_key;
361 cbdata.fval = cb_print_val;
363 walk_obj_cson(obj,&cbdata);
367 int cb_sniff_key(
const char *key,
int index,
cson_value *val,
void *cbdata){
370 kv = (
struct keyval *)(wcbd->data);
371 if(!strcmp(kv->key,key))
375 int cb_sniff_val(
cson_value *val,
int index,
void *cbdata){
380 switch(cson_value_type_id(val))
382 case CSON_TYPE_OBJECT:
385 rc = walk_obj_cson(obji,cbdata);
388 case CSON_TYPE_ARRAY:
391 rc = cson_value_fetch_array(val,&ar);
393 rc = walk_array_cson(ar,cbdata);
408 cbdata.fkey = cb_sniff_key;
409 cbdata.fval = cb_sniff_val;
410 walk_obj_cson(obj,&cbdata);
411 if(kv.cv) printf(
"sniffed key: %s\n",key);
415 int cb_gather_key(
const char *key,
int index,
cson_value *val,
void *cbdata){
420 if(!strcmp(key,
"command")){
422 if(!strcmp(key,
"init_pose")){
423 req->type = SSR_INITPOSE;
424 }
else if(!strcmp(key,
"posepose")){
425 req->type = SSR_POSEPOSE;
426 }
else if(!strcmp(key,
"posesnapshot")){
427 req->type = SSR_POSESNAPSHOT;
428 }
else if(!strcmp(key,
"")){
429 }
else if(!strcmp(key,
"")){
430 }
else if(!strcmp(key,
"")){
431 }
else if(!strcmp(key,
"")){
432 }
else if(!strcmp(key,
"")){
433 }
else if(!strcmp(key,
"")){
436 }
else if(!strcmp(key,
"level")){
438 rc = cson_value_fetch_integer(val, &ii );
440 }
else if(!strcmp(key,
"position")){
441 wcbd->arr = req->vec3;
443 }
else if(!strcmp(key,
"orientation")){
444 wcbd->arr = req->quat4;
446 }
else if(!strcmp(key,
"")){
447 }
else if(!strcmp(key,
"")){
451 int cb_gather_val(
cson_value *val,
int index,
void *cbdata){
456 switch(cson_value_type_id(val))
458 case CSON_TYPE_OBJECT:
461 rc = walk_obj_cson(obji,cbdata);
464 case CSON_TYPE_ARRAY:
467 rc = cson_value_fetch_array(val,&ar);
469 rc = walk_array_cson(ar,cbdata);
473 case CSON_TYPE_DOUBLE:
476 rc = cson_value_fetch_double(val, &dd );
477 ((
double *)wcbd->arr)[index] = dd;
487 cbdata.data = ssr_req;
488 cbdata.fkey = cb_gather_key;
489 cbdata.fval = cb_gather_val;
490 walk_obj_cson(obj,&cbdata);
494 int parse_json_and_gather_ssr_req(
char *strdata,
SSR_request *ssr_req){
499 rc = cson_parse_string(&root,strdata,strlen(strdata),NULL,&info);
501 if( cson_value_is_object(root) ) {
507 gather_ssr_req(root,ssr_req);
509 cson_value_free(root);
519 static int running_as_zonebalancer = 0;
523 #define strcasecmp _stricmp
525 void initialize_sockets(){
528 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
530 printf(
"WSAStartup failed: %d\n", iResult);
534 void initialize_sockets(){}
538 typedef struct Point {
558 int levels_available;
560 static zone *zones = NULL;
561 static ssr *ssrs = NULL;
562 void ssr_list_add(
ssr *s){
574 void zone_list_add(
zone *z){
588 #include <libxml/parser.h>
589 #include <libxml/tree.h>
590 static void print_element_names(xmlNode * a_node)
592 xmlNode *cur_node = NULL;
594 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
595 if (cur_node->type == XML_ELEMENT_NODE) {
597 printf(
"node type: Element, name: %s\n", cur_node->name);
598 for(cp = cur_node->properties; cp; cp = cp->next) {
600 printf(
"\tname=%s value=%s\n",cp->name,xmlGetNoNsProp(cur_node,cp->name));
604 print_element_names(cur_node->children);
608 static void load_zone_elements(xmlNode * a_node)
610 xmlNode *cur_node = NULL;
612 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
613 if (cur_node->type == XML_ELEMENT_NODE) {
616 printf(
"node type: Element, name: %s\n", cur_node->name);
617 if(!strcasecmp(cur_node->name,
"zone")){
620 for(cp = cur_node->properties; cp; cp = cp->next) {
622 value = xmlGetNoNsProp(cur_node,cp->name);
623 printf(
"\tname=%s value=%s\n",cp->name,value);
624 if(!strcasecmp(cp->name,
"ssr")){
625 z->ssrname = strdup(value);
626 }
else if(!strcmp(cp->name,
"name")){
627 z->name = strdup(value);
628 }
else if(!strcasecmp(cp->name,
"center")){
630 sscanf(value,
"%lf %lf %lf",&x,&y,&zz);
633 }
else if(!strcasecmp(cp->name,
"polygon")){
641 m = sscanf(q,
"%lf %lf",&x,&y);
654 z->poly = malloc(n*
sizeof(
Point));
655 memcpy(z->poly,points,n*
sizeof(
Point));
660 }
else if(!strcasecmp(cur_node->name,
"ssrserver")){
662 ssr *s = malloc(
sizeof(
ssr));
663 for(cp = cur_node->properties; cp; cp = cp->next) {
664 value = xmlGetNoNsProp(cur_node,cp->name);
665 printf(
"\tname=%s value=%s\n",cp->name,value);
666 if(!strcasecmp(cp->name,
"name")){
667 s->name = strdup(value);
668 }
else if(!strcasecmp(cp->name,
"ip")){
669 s->ip = strdup(value);
670 }
else if(!strcasecmp(cp->name,
"port")){
671 s->port = strdup(value);
678 load_zone_elements(cur_node->children);
681 zone *find_zone_by_name(
char *name){
684 if(!strcmp(z->name,name))
696 inside = cn_PnPoly( P, z->poly, z->n -1 );
703 void test_pointinpoly(){
707 zn = find_zone_by_name(
"StrandEnDuin");
712 z = find_zone_by_point(p);
714 printf(
"inside polygon %s\n",z->name);
716 printf(
"polygon not found\n");
721 void assign_ssr_by_name(){
729 if(!strcasecmp(z->ssrname,s->name)){
738 static int testing_pointinpoly = 1;
739 void load_polys(
char *filename){
741 xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;
742 doc = xmlReadFile(filename, NULL, 0);
744 fprintf(stderr,
"Failed to parse %s\n", filename);
747 root_node = xmlDocGetRootElement(doc);
749 load_zone_elements(root_node);
750 assign_ssr_by_name();
752 if(testing_pointinpoly)
754 printf(
"done test_pointinpoly\n");
776 static struct ssr_leaf {
779 double xoff, yoff,zoff;
792 #include <microhttpd.h>
797 #define PAGE "<html><head><title>libmicrohttpd demo</title>"\
798 "</head><body>libmicrohttpd demo</body></html>"
804 #define POSTBUFFERSIZE 512
805 #define MAXNAMESIZE 20
806 #define MAXANSWERSIZE 512
816 struct MHD_PostProcessor *postprocessor;
819 const char *askpage =
"<html><body>\
820 What's your name, Sir?<br>\
821 <form action=\"/namepost\" method=\"post\">\
822 <input name=\"name\" type=\"text\"\
823 <input type=\"submit\" value=\" Send \"></form>\
826 const char *greetingpage =
827 "<html><body><h1>Welcome, %s!</center></h1></body></html>";
829 const char *errorpage =
830 "<html><body>This doesn't seem to be right.</body></html>";
834 send_page (
struct MHD_Connection *connection,
const char *page,
int len)
837 struct MHD_Response *response;
840 response = MHD_create_response_from_buffer (len, (
void *) page, MHD_RESPMEM_PERSISTENT);
844 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
845 MHD_destroy_response (response);
849 #define MAXPOSESIZE 255
850 static void jsonPose2double(
double *quat4,
double *vec3,
char *data){
851 sscanf(data,
"[%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf]",
852 &quat4[0],&quat4[1],&quat4[2],&quat4[3],
853 &vec3[0],&vec3[1],&vec3[2]);
855 static void doublePose2json(
double *quat4,
double *vec3,
char *data,
int MAX){
856 sprintf(data,
"[%lf, %lf, %lf, %lf, %lf, %lf, %lf]",
857 quat4[0],quat4[1],quat4[2],quat4[3],
858 vec3[0],vec3[1],vec3[2]);
860 static void doublePose2jsonB(
double *quat4,
double *vec3,
char *data,
int MAX){
867 cson_value * objroot = cson_value_new_object();
871 cson_object * obj = cson_value_get_object(objroot);
883 arpos = cson_value_new_array();
884 cson_object_set( obj,
"position", arpos );
885 ar = cson_value_get_array(arpos);
887 cson_array_set( ar, i, cson_value_new_double(vec3[i]) );
888 arori = cson_value_new_array();
889 cson_object_set( obj,
"orientation", arori );
890 ar = cson_value_get_array(arori);
892 cson_array_set( ar, i, cson_value_new_double(quat4[i]) );
897 rc = cson_output_buffer( objroot, &buf, NULL );
908 data[buf.
used] =
'\0';
911 cson_buffer_reserve( &buf, 0 );
923 cson_value_free( objroot );
927 static char* getSnapshot(
int *len){
931 char *filename =
"2.jpg";
932 if (stat(filename, &ss) < 0) {
933 printf(
"load_file_read: could not stat: %s\n", filename);
937 printf(
"load_file_read: file is empty %s\n", filename);
940 if (ss.st_size > SSIZE_MAX) {
944 blocksz = ss.st_size;
946 fd = open(filename, O_RDONLY | O_BINARY | O_BLOCK);
948 printf(
"load_file_read: could not open: %s\n", filename);
955 char *blob = malloc(blocksz);
956 read(fd,blob,blocksz);
967 #ifndef WIN32_LEAN_AND_MEAN
968 #define WIN32_LEAN_AND_MEAN
970 #define strdup _strdup
971 #include <winsock2.h>
972 #include <ws2tcpip.h>
973 #define SHUT_RDWR SD_BOTH
975 #define snprintf _snprintf
977 #define STRTOK_S strtok_s
978 int sockwrite(SOCKET s,
const char *buf,
int len){
979 return send(s,buf,len,0);
981 int sockread(SOCKET s,
const char *buf,
int len){
982 return recv(s,buf,len,0);
986 #include <sys/socket.h>
987 #include <netinet/in.h>
989 #define STRTOK_S strtok_r
990 int sockwrite(SOCKET s,
const char *buf,
int len){
991 return write(s,buf,len);
993 int sockread(SOCKET s,
const char *buf,
int len){
994 return recv(s,buf,len,0);
998 int socket_connect(
char *host,
int port){
1000 struct sockaddr_in addr;
1002 unsigned short port16;
1003 struct addrinfo *result = NULL,
1008 ZeroMemory( &hints,
sizeof(hints) );
1009 hints.ai_family = AF_UNSPEC;
1010 hints.ai_socktype = SOCK_STREAM;
1011 hints.ai_protocol = IPPROTO_TCP;
1013 iResult = getaddrinfo(
"localhost",
"8081", &hints, &result);
1015 printf(
"getaddrinfo failed: %d\n", iResult);
1020 if((hp = gethostbyname(
"localhost")) == NULL){
1021 perror(
"gethostbyname");
1025 memcpy(&addr.sin_addr,hp->h_addr, hp->h_length);
1027 addr.sin_port = htons(port);
1028 addr.sin_family = AF_INET;
1029 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1030 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (
const char *)&on,
sizeof(
int));
1033 perror(
"setsockopt");
1037 if(connect(sock, (
struct sockaddr *)&addr,
sizeof(
struct sockaddr_in)) == -1){
1049 int build_http_post(
char *post,
char *host,
int port,
int lencontent,
char *useragent,
char *origin,
char *referer)
1053 post_fmt =
"POST /pose HTTP/1.1\r\nHost: %s:%d\r\nConnection: keep-alive\r\nContent-Length: %d\r\nContent-type: application/x-www-form-urlencoded\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.8\r\n\r\n";
1054 sprintf(post, post_fmt, host, port,lencontent);
1060 #define BUFFER_SIZE 32768
1068 int reverse_proxy(
char *host,
char *port,
char *ssr_command,
char *key,
char *request,
int size,
char **response)
1072 char buffer[BUFFER_SIZE];
1073 char content_length[100];
1082 fd = socket_connect(host, atoi(port));
1083 if (fd == INVALID_SOCKET) {
1084 printf(
"Error at socket(): %ld\n", WSAGetLastError());
1093 char *answer, *temp;
1094 int answermax = BUFFER_SIZE;
1095 answer = malloc(answermax);
1107 int lencontent = size + strlen(key) + 1;
1108 lenpost = build_http_post(post, host, atoi(port), lencontent, NULL, NULL, NULL);
1109 sprintf(&post[lenpost],
"%s=",key);
1110 lenpost += strlen(key)+1;
1111 memcpy(&post[lenpost],request,size);
1113 memcpy(&post[lenpost],
"\r\n",3);
1115 sockwrite(fd,post,lenpost);
1119 memset(buffer,0, BUFFER_SIZE);
1126 while( li = sockread(fd, buffer, BUFFER_SIZE - 1) ){
1127 if(lr + li > answermax ){
1129 answer = realloc(answer,answermax);
1132 memcpy(r,buffer,li);
1135 memset(buffer,0, BUFFER_SIZE);
1139 char *cl = strstr(answer,
"Content-Length:");
1141 cl += strlen(
"Content-Length:");
1146 if(lencontent && !offcontent){
1147 char *cd = strstr(answer,
"\r\n\r\n");
1150 cd = strstr(answer,
"\n\n");
1153 if(cd) offcontent = cd - answer;
1156 if(lencontent && offcontent)
1157 if(lr >= offcontent + lencontent)
break;
1160 shutdown(fd, SHUT_RDWR);
1162 if(lencontent && offcontent){
1163 char *finalanswer = malloc(lencontent+1);
1164 memcpy(finalanswer,&answer[offcontent],lencontent);
1165 finalanswer[lencontent] =
'\0';
1168 answer = finalanswer;
1174 int sniff_and_foreward(
char *ssr_command,
char *key,
char *data,
int size,
char **answerstring)
1183 jsonPose2double(quat4,vec3,data);
1187 z = find_zone_by_point(posexy);
1192 ssri = (
ssr*)z->ssr;
1194 len = reverse_proxy(ssri->ip, ssri->port, ssr_command, key, data, size, answerstring);
1206 static int iterate_post_zb (
void *coninfo_cls,
enum MHD_ValueKind kind,
const char *key,
1207 const char *filename,
const char *content_type,
1208 const char *transfer_encoding,
const char *data,
1209 uint64_t off,
size_t size)
1213 printf(
"Key=%s\n",key);
1214 printf(
"filename=%s\n",filename);
1215 printf(
"content_type=%s\n",content_type);
1216 printf(
"transfer_encoding=%s\n",transfer_encoding);
1217 printf(
"data=%s\n",data);
1219 if (0 == strcmp (key,
"posepose"))
1221 if ((size > 0) && (size <= MAXPOSESIZE))
1228 answerstring = NULL;
1229 ssr_req.type = SSR_POSEPOSE;
1230 len = sniff_and_foreward(
"pose",key,data,size,&answerstring);
1231 con_info->answerstring = answerstring;
1232 con_info->len = len;
1234 else con_info->answerstring = NULL;
1238 if (0 == strcmp (key,
"posesnapshot"))
1240 if ((size > 0) && (size <= MAXPOSESIZE))
1245 ssr_req.type = SSR_POSESNAPSHOT;
1246 answerstring = NULL;
1247 len = sniff_and_foreward(
"pose",key,data,size,&answerstring);
1248 con_info->answerstring = answerstring;
1249 con_info->len = len;
1251 else con_info->answerstring = NULL;
1261 static int iterate_post (
void *coninfo_cls,
enum MHD_ValueKind kind,
const char *key,
1262 const char *filename,
const char *content_type,
1263 const char *transfer_encoding,
const char *data,
1264 uint64_t off,
size_t size)
1268 fprintf(stdout,
"Key=%s\n",key);
1269 fprintf(stdout,
"filename=%s\n",filename);
1270 fprintf(stdout,
"content_type=%s\n",content_type);
1271 fprintf(stdout,
"transfer_encoding=%s\n",transfer_encoding);
1272 fprintf(stdout,
"data=%s\n",data);
1275 if (0 == strcmp (key,
"init_pose"))
1277 fprintf(stdout,
"A");
1278 if ((size > 0) && (size <= MAXPOSESIZE))
1283 answerstring = malloc (MAXANSWERSIZE);
1284 fprintf(stdout,
"B");
1287 ssr_req.type = SSR_INITPOSE;
1292 fprintf(stdout,
"C");
1293 dllFreeWRL_SSRserver_enqueue_request_and_wait(fwctx, &ssr_req);
1294 fprintf(stdout,
"D");
1295 ssr_req.vec3[0] += ssrleaf.xoff;
1296 ssr_req.vec3[1] += ssrleaf.yoff;
1297 ssr_req.vec3[2] += ssrleaf.zoff;
1298 doublePose2json(ssr_req.quat4,ssr_req.vec3, answerstring, MAXANSWERSIZE);
1299 fprintf(stdout,
"E");
1301 con_info->answerstring = answerstring;
1302 con_info->len = strlen(answerstring);
1303 for(
int k=0;k<con_info->len;k++)
1304 fprintf(stdout,
"%c",con_info->answerstring[k]);
1305 fprintf(stdout,
" len=%d\n",con_info->len);
1307 else con_info->answerstring = NULL;
1312 if (0 == strcmp (key,
"posepose"))
1314 if ((size > 0) && (size <= MAXPOSESIZE))
1319 answerstring = malloc (MAXANSWERSIZE);
1320 if (!answerstring)
return MHD_NO;
1321 ssr_req.type = SSR_POSEPOSE;
1322 jsonPose2double(ssr_req.quat4,ssr_req.vec3,data);
1323 ssr_req.vec3[0] -= ssrleaf.xoff;
1324 ssr_req.vec3[1] -= ssrleaf.yoff;
1325 ssr_req.vec3[2] -= ssrleaf.zoff;
1326 dllFreeWRL_SSRserver_enqueue_request_and_wait(fwctx, &ssr_req);
1327 ssr_req.vec3[0] += ssrleaf.xoff;
1328 ssr_req.vec3[1] += ssrleaf.yoff;
1329 ssr_req.vec3[2] += ssrleaf.zoff;
1330 doublePose2json(ssr_req.quat4,ssr_req.vec3, answerstring, MAXANSWERSIZE);
1332 con_info->answerstring = answerstring;
1333 con_info->len = strlen(answerstring);
1336 else con_info->answerstring = NULL;
1340 if (0 == strcmp (key,
"posesnapshot"))
1342 if ((size > 0) && (size <= MAXPOSESIZE))
1346 ssr_req.type = SSR_POSESNAPSHOT;
1347 ssr_req.blob = NULL;
1349 jsonPose2double(ssr_req.quat4,ssr_req.vec3,data);
1350 ssr_req.vec3[0] -= ssrleaf.xoff;
1351 ssr_req.vec3[1] -= ssrleaf.yoff;
1352 ssr_req.vec3[2] -= ssrleaf.zoff;
1353 dllFreeWRL_SSRserver_enqueue_request_and_wait(fwctx, &ssr_req);
1354 con_info->answerstring = ssr_req.blob;
1355 con_info->len = ssr_req.len;
1357 else con_info->answerstring = NULL;
1365 void request_completed (
void *cls,
struct MHD_Connection *connection,
1367 enum MHD_RequestTerminationCode toe)
1371 if (NULL == con_info)
return;
1372 if (con_info->connectiontype == POST)
1374 MHD_destroy_post_processor (con_info->postprocessor);
1375 if (con_info->answerstring) free (con_info->answerstring);
1382 int haveNasties(
char *url){
1392 int urllen, nasties = 0;
1393 urllen = strlen(url);
1394 for(
int i=0;i<urllen;i++){
1396 if(url[i]==
'.' && url[i+1] ==
'.') nasties++;
1397 if(url[i]==
':') nasties++;
1398 if(url[i]==
'\b') nasties++;
1406 static int answer_to_connection (
void *cls,
struct MHD_Connection *connection,
1408 const char *method,
const char *version,
1409 const char *upload_data,
1410 size_t *upload_data_size,
void **con_cls)
1413 printf(
"\nurl=%s\n",url);
1414 printf(
"method=%s\n",method);
1415 printf(
"version=%s\n\n",version);
1416 printf(
"upload_data=%s\n",upload_data);
1418 if(NULL == *con_cls)
1423 if (NULL == con_info)
1425 con_info->answerstring = NULL;
1428 if (0 == strcmp (method,
"POST"))
1430 if(running_as_zonebalancer)
1431 con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE,
1432 iterate_post_zb, (
void*) con_info);
1434 con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE,
1435 iterate_post, (
void*) con_info);
1436 if (NULL == con_info->postprocessor)
1441 con_info->connectiontype = POST;
1444 con_info->connectiontype = GET;
1447 *con_cls = (
void*) con_info;
1451 if (0 == strcmp (method,
"GET"))
1455 struct MHD_Response * response;
1459 char *filename = &url[1];
1460 if (stat(filename, &ss) < 0) {
1461 printf(
"load_file_read: could not stat: %s\n", filename);
1465 printf(
"load_file_read: file is empty %s\n", filename);
1468 if (ss.st_size > SSIZE_MAX) {
1470 blocksz = SSIZE_MAX;
1472 blocksz = ss.st_size;
1474 fd = open(filename, O_RDONLY | O_BINARY | O_BLOCK);
1476 printf(
"load_file_read: could not open: %s\n", filename);
1483 char *blob = malloc(blocksz);
1484 read(fd,blob,blocksz);
1487 response = MHD_create_response_from_data(blocksz,(
void*) blob, MHD_NO, MHD_YES);
1490 ret = MHD_queue_response(connection,MHD_HTTP_OK,response);
1491 MHD_destroy_response(response);
1496 if (0 == strcmp (method,
"POST"))
1499 if (*upload_data_size != 0)
1501 MHD_post_process (con_info->postprocessor, upload_data,
1503 *upload_data_size = 0;
1507 if (NULL != con_info->answerstring)
1508 return send_page (connection, con_info->answerstring, con_info->len);
1511 return send_page(connection, errorpage, strlen(errorpage));
1515 static int ahc_echo(
void * cls,
1516 struct MHD_Connection * connection,
1518 const char * method,
1519 const char * version,
1520 const char * upload_data,
1521 size_t * upload_data_size,
1525 const char * page = cls;
1526 struct MHD_Response * response;
1529 if (!strcmp(method,
"POST"))
1533 printf(
"url=%s\n",url);
1534 printf(
"method=%s\n",method);
1535 printf(
"version=%s\n",version);
1536 printf(
"upload_data=%s\n",upload_data);
1539 if(!strcmp(method,
"GET"))
1550 if (0 != *upload_data_size)
1553 printf(
" %p ",connection);
1554 if(!strcmp(url,
"/") || haveNasties(url)){
1556 response = MHD_create_response_from_data(strlen(page),
1565 char *filename = &url[1];
1566 if (stat(filename, &ss) < 0) {
1567 printf(
"load_file_read: could not stat: %s\n", filename);
1571 printf(
"load_file_read: file is empty %s\n", filename);
1574 if (ss.st_size > SSIZE_MAX) {
1576 blocksz = SSIZE_MAX;
1578 blocksz = ss.st_size;
1580 fd = open(filename, O_RDONLY | O_BINARY | O_BLOCK);
1582 printf(
"load_file_read: could not open: %s\n", filename);
1589 char *blob = malloc(blocksz);
1590 read(fd,blob,blocksz);
1593 response = MHD_create_response_from_data(blocksz,(
void*) blob, MHD_NO, MHD_YES);
1597 ret = MHD_queue_response(connection,MHD_HTTP_OK,response);
1598 MHD_destroy_response(response);
1609 int main0(
int argc,
char ** argv) {
1627 struct MHD_Daemon * d;
1628 char *portstr, *url, *publicpath;
1630 if(strstr(argv[0],
"projectfiles")){
1633 publicpath = strdup(argv[0]);
1634 pf = strstr(publicpath,
"projectfiles");
1635 strcpy(pf,
"src/SSR/public");
1639 publicpath = strdup(argv[0]);
1640 pf = strstr(publicpath,
"SSRserver.exe");
1641 strcpy(pf,
"public");
1647 printf(
"%s PORT\n",portstr);
1656 if(!strcmp(url,
"--zonebalancer")){
1657 load_polys(
"..\\zonebalancer.xml");
1658 running_as_zonebalancer = 1;
1659 initialize_sockets();
1661 for(
int k=3;k<argc;k++)
1663 char *arg = argv[k];
1664 if(!strncmp(arg,
"--",2))
1665 if(!strncmp(&arg[3],
"off=",4)){
1666 if(!strncmp(&arg[2],
"x",1)){
1667 sscanf(&arg[7],
"%lf",&ssrleaf.xoff);
1668 }
else if(!strncmp(&arg[2],
"y",1)){
1669 sscanf(&arg[7],
"%lf",&ssrleaf.yoff);
1670 }
else if(!strncmp(&arg[2],
"z",1)){
1671 sscanf(&arg[7],
"%lf",&ssrleaf.zoff);
1674 if(!strcmp(arg,
"--publicpath")){
1676 publicpath = strdup(argv[k+1]);
1684 if(running_as_zonebalancer)
1689 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
1698 printf(
"public path: %s\n",publicpath);
1702 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION,
1706 &answer_to_connection,
1708 MHD_OPTION_NOTIFY_COMPLETED,
1715 if(!running_as_zonebalancer)
1717 printf(
"Press Enter to stop libmicrohttp deamon and exit:");
1719 if(!running_as_zonebalancer)
1726 int main(
int argc,
char ** argv) {
1727 int iret = main0(argc, argv);
1729 printf(
"Press Enter to exit:");
unsigned char * mem
The memory allocated for and owned by this buffer.
The core value type of this API.
cson_array is an opaque handle to an Array value.
A class for holding JSON parser information.
cson_size_t used
The number of bytes "used" by this object.
A key/value pair collection.
An iterator type for traversing object properties.
Strings are allocated as an instances of this class with N+1 trailing bytes, where N is the length of...
cson_object is an opaque handle to an Object value.