diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8862c08 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.[oa] diff --git a/build/httpd b/build/httpd index e7d5ab8..c679977 100755 Binary files a/build/httpd and b/build/httpd differ diff --git a/build/httpd.dSYM/Contents/Resources/DWARF/httpd b/build/httpd.dSYM/Contents/Resources/DWARF/httpd index 0f731fb..6c505f5 100644 Binary files a/build/httpd.dSYM/Contents/Resources/DWARF/httpd and b/build/httpd.dSYM/Contents/Resources/DWARF/httpd differ diff --git a/build/plugins/dummy.dylib b/build/plugins/dummy.dylib index 0505c15..9294138 100755 Binary files a/build/plugins/dummy.dylib and b/build/plugins/dummy.dylib differ diff --git a/build/plugins/fileman.dylib b/build/plugins/fileman.dylib index 7cb8019..8100897 100755 Binary files a/build/plugins/fileman.dylib and b/build/plugins/fileman.dylib differ diff --git a/build/plugins/pluginsman.dylib b/build/plugins/pluginsman.dylib index 6650feb..1c53b48 100755 Binary files a/build/plugins/pluginsman.dylib and b/build/plugins/pluginsman.dylib differ diff --git a/http_server.c b/http_server.c index 133e00f..ab00d08 100644 --- a/http_server.c +++ b/http_server.c @@ -9,7 +9,7 @@ void accept_request(int client) char buf[1024]; int numchars; char method[255]; - char url[255]; + char url[4096]; char path[512]; size_t i, j; struct stat st; diff --git a/plugin_manager.c b/plugin_manager.c index eb39df6..3109515 100644 --- a/plugin_manager.c +++ b/plugin_manager.c @@ -349,7 +349,7 @@ int read_buf(int sock, char*buf,int size) struct plugin_entry *plugin_lookup(char *s) { struct plugin_entry *np; - for (np = plugin_table[hash(s)]; np != NULL; np = np->next) + for (np = plugin_table[hash(s, HASHSIZE)]; np != NULL; np = np->next) if (strcmp(s, np->pname) == 0) return np; /* found */ return NULL; /* not found */ @@ -371,7 +371,7 @@ struct plugin_entry *plugin_load(char *name) return NULL; if ((np->handle = plugin_from_file(name)) == NULL) return NULL; - hashval = hash(name); + hashval = hash(name,HASHSIZE); np->next = plugin_table[hashval]; plugin_table[hashval] = np; } else /* already there */ diff --git a/plugins/dictionary.c b/plugins/dictionary.c index f0d2275..29f3260 100644 --- a/plugins/dictionary.c +++ b/plugins/dictionary.c @@ -1,3 +1,26 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 "dictionary.h" dictionary dict() @@ -7,18 +30,11 @@ dictionary dict() d[i] = NULL; return d; } -unsigned hash(const char* key) -{ - unsigned hashval; - for (hashval = 0; *key != '\0'; key++) - hashval = *key + 31 * hashval; - return hashval % HASHSIZE; -} association dlookup(dictionary dic,const char* key) { association np; if(dic == NULL) return NULL; - for (np = dic[hash(key)]; np != NULL; np = np->next) + for (np = dic[hash(key,DHASHSIZE)]; np != NULL; np = np->next) { if(!np || !np->key) { @@ -38,55 +54,23 @@ association __put_el_with_key(dictionary dic, const char* key) np = (association) malloc(sizeof(*np)); if (np == NULL || (np->key = strdup(key)) == NULL) return NULL; - hashval = hash(key); + hashval = hash(key, DHASHSIZE); np->next = dic[hashval]; dic[hashval] = np; } return np; } -association dput(dictionary dic,const char* key, const char* value) -{ - if(IS_INT(value)) - return dput_i(dic,key,atoi(value)); - else if(IS_FLOAT(value)) - return dput_f(dic,key,atof(value)); - else - return dput_s(dic,key,value); -} -association dput_s(dictionary dic,const char* key, const char* value) +association dput(dictionary dic,const char* key, void* value) { association np = __put_el_with_key(dic,key); if(np == NULL) return NULL; - if(value == NULL) np->value.s=""; - else if ((np->value.s = strdup(value)) == NULL) - return NULL; - return np; -} -association dput_i(dictionary dic,const char* key, int value) -{ - association np = __put_el_with_key(dic,key); - if(np == NULL) return NULL; - np->value.i = value; - return np; -} -association dput_f(dictionary dic,const char* key, float value) -{ - association np = __put_el_with_key(dic,key); - if(np == NULL) return NULL; - np->value.f = value; - return np; -} -association dput_p(dictionary dic,const char* key, void* value) -{ - association np = __put_el_with_key(dic,key); - if(np == NULL) return NULL; - np->value.p = value; + np->value = value; return np; } int dremove(dictionary dic, const char* key) { if(dic == NULL) return 0; - int hashval = hash(key); + int hashval = hash(key, DHASHSIZE); association np = dic[hashval]; if(np!=NULL && strcmp(key,np->key)==0) { @@ -102,36 +86,11 @@ int dremove(dictionary dic, const char* key) return 0; /* not found */ } -/** - * Get the string data by key - * @param dic the dictionary - * @param key @ - * @return the string value - */ -char* dvalue(dictionary dic,const char* key) +void* dvalue(dictionary dic, const char* key) { association as = dlookup(dic,key); if(as == NULL) return NULL; - return as->value.s; -} - -int dvalue_i(dictionary dic, const char* key) -{ - association as = dlookup(dic,key); - if(as == NULL) return 0; - return as->value.i; -} -float dvalue_f(dictionary dic, const char* key) -{ - association as = dlookup(dic,key); - if(as == NULL) return 0; - return as->value.f; -} -void* dvalue_p(dictionary dic, const char* key) -{ - association as = dlookup(dic,key); - if(as == NULL) return NULL; - return as->value.p; + return as->value; } void freedict(dictionary dic){free(dic);} diff --git a/plugins/dictionary.h b/plugins/dictionary.h index 169adfc..1675df4 100644 --- a/plugins/dictionary.h +++ b/plugins/dictionary.h @@ -1,5 +1,30 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 DICTIONARY_H +#define DICTIONARY_H + #include "utils.h" -#define HASHSIZE 255 #define for_each_assoc(assoc, dic) \ for(int i = 0; i < HASHSIZE; i++) \ for(assoc = dic[i];assoc!= NULL; assoc = assoc->next) @@ -10,29 +35,17 @@ typedef struct __assoc{ struct __assoc *next; char *key; - union - { - int i; - char* s; - float f; - void* p; - } value; + void* value; //char *value; } * association; typedef association* dictionary; dictionary dict(); -unsigned hash(const char*); association dlookup(dictionary,const char*); -char* dvalue(dictionary, const char*); -int dvalue_i(dictionary, const char*); -float dvalue_f(dictionary, const char*); -void* dvalue_p(dictionary, const char*); -association dput(dictionary,const char*, const char*); -association dput_s(dictionary,const char*, const char*); -association dput_i(dictionary,const char*, int); -association dput_f(dictionary,const char*, float); -association dput_p(dictionary,const char*, void*); +void* dvalue(dictionary, const char*); +association dput(dictionary,const char*, void*); int dremove(dictionary, const char*); void freedict(dictionary); -void stest(const char* ); \ No newline at end of file +void stest(const char* ); + +#endif \ No newline at end of file diff --git a/plugins/fileman/fileman.c b/plugins/fileman/fileman.c index 0689f5d..97a5519 100644 --- a/plugins/fileman/fileman.c +++ b/plugins/fileman/fileman.c @@ -15,11 +15,11 @@ char* folder_list_from(const char* aPath) { for(list np = path; np != NULL; np=np->next) { - route = __s("%s%s%s", route, DIR_SEP, np->e.value.s); + route = __s("%s%s%s", route, DIR_SEP, np->value.s); if(flist == NULL) - flist = __s(FOLLIST, np->e.value.s,route); + flist = __s(FOLLIST, np->value.s,route); else - flist = __s("%s,%s", flist,__s(FOLLIST,np->e.value.s,route)); + flist = __s("%s,%s", flist,__s(FOLLIST,np->value.s,route)); } } free(path); diff --git a/plugins/fileman/fileman.c.o b/plugins/fileman/fileman.c.o index a0e5a9c..f6b681d 100644 Binary files a/plugins/fileman/fileman.c.o and b/plugins/fileman/fileman.c.o differ diff --git a/plugins/list.c b/plugins/list.c index 2a679e1..a023506 100644 --- a/plugins/list.c +++ b/plugins/list.c @@ -1,35 +1,91 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 "list.h" -Atom __atom() +list list_init() { - Atom ret = (Atom){.type = T_UNDEF}; - return ret; -} -Atom atom(int type) -{ - Atom ret = (Atom){.type = type}; - return ret; -} -list __list() -{ - list ret = malloc(sizeof *ret); - ret->e.type = T_UNDEF; + list ret = (list)malloc(sizeof *ret); + ret->type = RPC_TYPE_NIL; ret->next = NULL; return ret; } - -list last(list l) +void list_put(list* l, item it) { - list p = l; - while( p && p->next != NULL) + if(*l == NULL || (*l)->type == RPC_TYPE_NIL) + { + *l = it; + return ; + } + item np = list_last(*l); + np->next = it; +} +void list_put_special(list* l, const char* str) +{ + item v; + if(IS_INT(str)) + { + v = list_item(RPC_TYPE_INT); + v->value.i = atoi(str); + } + else if(IS_FLOAT(str)) + { + v = list_item(RPC_TYPE_DOUBLE); + v->value.d = (double)atof(str); + } + else + { + v = list_item(RPC_TYPE_STRING); + v->value.s = strdup(str); + } + list_put(l,v); +} +item list_last(list l) +{ + item p = l; + while(p && p->next != NULL) p = p->next; return p; } - -int size(list l) +int list_remove(list l,int idx) { - int i = 0; - list np = l; + if(l==NULL) return 0; + if(idx <0 || idx >= list_size(l)) return 0; + if(idx == 0) + { + l=l->next; + return 1; + } + item np = list_at(l,idx-1); + if(np == NULL) return 0; + if(np->next == NULL) return 1; + np->next = np->next->next; +} +int list_size(list l) +{ + if(l == NULL || l->type == RPC_TYPE_NIL) return 0; + int i=0; + item np = l; while(np) { np = np->next; @@ -37,128 +93,147 @@ int size(list l) } return i; } -Atom lvalueAt(list l,int idx) +char* as_string(list l) { - list np = lat(l,idx); - if(np == NULL) return __atom(); - return np->e; -} - -list lat(list l,int idx) -{ - int i = 0; - list np = l; - while(np && np->next != NULL) + char* str = ""; + if(l != NULL && l->type != RPC_TYPE_NIL) { - if(i== idx) + switch(l->type) + { + case RPC_TYPE_BASE64: + str = __s("b64:%s", l->value.b64); + break; + + case RPC_TYPE_BOOL: + str = __s("bool:%d", l->value.b); + break; + + case RPC_TYPE_DOUBLE: + str = __s("double:%lf", l->value.d); + break; + + case RPC_TYPE_DATE: + str = __s("date:%s", l->value.date); + break; + + case RPC_TYPE_INT: + case RPC_TYPE_I4: + str = __s("int:%d", l->value.i); + break; + + case RPC_TYPE_STRING: + str = __s("string:%s", l->value.s); + break; + + case RPC_TYPE_ARRAY: + str = __s("[%s]", as_string(l->value.array)); + break; + default: + str = ""; + break; + } + item np = l->next; + if(np) + { + str = __s("%s,\n%s", str, as_string(np)); + } + return str; + } + return "[empty]"; +} +item list_at(list l,int idx) +{ + if(l == NULL || idx<0 || idx>= list_size(l)) + return NULL; + int i=0; + item np = l; + while(np) + { + if(i==idx) return np; np = np->next; i++; } return NULL; } -list lput(list* l, Atom a) +item list_item(int type) { - list new_item = malloc(sizeof *new_item); - new_item->e = a; - new_item->next = NULL; - if((*l) == NULL || (*l)->e.type == T_UNDEF) - { - (*l) = new_item; - return (*l); - } - list np = last(*l); - np->next = new_item; - return np->next; + item ret = (item)malloc(sizeof *ret); + ret->type = type; + ret->next = NULL; + return ret; } -int lremove(list l,int idx) -{ - if(l == NULL) return 0; - - if(idx == 0) - { - l = l->next; - return 1; - } - list np = lat(l,idx-1); - if(np == NULL) return 0; - if(np->next == NULL) return 1; - np->next = np->next->next; - return 1; -} -char* to_string(Atom a) -{ - switch(a.type) - { - case T_STR: - return a.value.s; - break; - case T_INT: - return __s("%d",a.value.i); - break; - case T_FLOAT: - return __s("%f",a.value.f); - break; - case T_FIELD: - return f2s(a.value.fi); - break; - default: - return ""; - } -} - char* fv2s(field f) -{ - switch(f.type) - { - case T_STR: - return __s("%s",f.value.s); - break; - case T_INT: - return __s("%d",f.value.i); - break; - case T_FLOAT: - return __s("%f",f.value.f); - break; - default: - return ""; - } -} -char* f2s(field f) -{ - switch(f.type) - { - case T_STR: - return __s("%s='%s'",f.name,f.value.s); - break; - case T_INT: - return __s("%s=%d",f.name,f.value.i); - break; - case T_FLOAT: - return __s("%s=%f",f.name,f.value.f); - break; - default: - return ""; - } -} - list split(const char* str, const char* delim) { if(str == NULL || delim == NULL) return NULL; char* str_cpy = strdup(str); char* token; - Atom v = atom(T_STR); - list l = __list(); + list l = list_init(); while((token = strsep(&str_cpy,delim))) { if(strlen(token) > 0) { - v = (Atom){.value.s = strdup(token)}; - lput(&l,v); + list_put_special(&l,token); } } free(str_cpy); - if(l->e.type== T_UNDEF) + if(l->type== RPC_TYPE_NIL) return NULL; - return l; } +void list_put_i(list* l,int v) +{ + item it = list_item(RPC_TYPE_INT); + it->value.i = v; + list_put(l,it); +} +void list_put_d(list* l,double v) +{ + item it = list_item(RPC_TYPE_DOUBLE); + it->value.d = v; + list_put(l,it); +} +void list_put_b(list* l,int v) +{ + item it = list_item(RPC_TYPE_BOOL); + it->value.b = v; + list_put(l,it); +} +void list_put_b64(list* l,const char* v) +{ + item it = list_item(RPC_TYPE_BASE64); + it->value.b64 = strdup(v); + list_put(l,it); +} +void list_put_date(list* l,const char* v) +{ + item it = list_item(RPC_TYPE_DATE); + it->value.date = strdup(v); + list_put(l,it); +} +void list_put_s(list* l,const char* v) +{ + item it = list_item(RPC_TYPE_STRING); + it->value.s = strdup(v); + list_put(l,it); +} +void list_put_array(list* l,list v) +{ + item it = list_item(RPC_TYPE_ARRAY); + it->value.array = v; + list_put(l,it); +} +void list_free(list *l) +{ + item curr; + while ((curr = (*l)) != NULL) { + (*l) = (*l)->next; + if(curr->type == RPC_TYPE_ARRAY) + list_free(&curr->value.array); + free (curr); + } +} +int list_empty(list l) +{ + return l== NULL || l->type == RPC_TYPE_NIL; +} \ No newline at end of file diff --git a/plugins/list.h b/plugins/list.h index 91071b4..af9ffc9 100644 --- a/plugins/list.h +++ b/plugins/list.h @@ -1,57 +1,61 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 LIST_H +#define LIST_H #include "utils.h" - -#define T_STR 0 -#define T_INT 1 -#define T_FLOAT 2 -#define T_FIELD 3 -#define T_UNDEF 4 - - -typedef struct -{ - char* name; - int type; - union - { - int i; - float f; - char* s; +#define list item +typedef struct __item{ + int type; + union{ + int i; + int b; + char* s; + double d; + char* date; + char* b64; + struct __item* array; } value; -} field; + struct __item* next; +}*item; -typedef struct -{ - int type; - union - { - int i; - float f; - char* s; - field fi; - } value; -} Atom; - -typedef struct _item -{ - Atom e; - struct _item* next; -} *list ; - - -list __list(); -list lput(list*,Atom); -list last(list); -int lremove(list,int); -int size(list); -list lat(list,int); -Atom lvalueAt(list, int); -Atom atom(int type); -char* a2s(Atom); -char* f2s(field); -char* fv2s(field); +list list_init(); +void list_put(list*,item); +void list_put_i(list*,int); +void list_put_d(list*,double); +void list_put_b(list*,int); +void list_put_b64(list*,const char*); +void list_put_date(list*,const char*); +void list_put_s(list*,const char*); +void list_put_array(list*,list); +item list_last(list); +int list_remove(list,int); +int list_size(list); +item list_at(list,int); +int list_empty(list); +item list_item(int type); list split(const char*, const char*); - - - - - +char* as_string(list); +void list_put_special(list*, const char*); +void list_free(list *); +#endif \ No newline at end of file diff --git a/plugins/plugin.c b/plugins/plugin.c index 43355ff..a36f975 100644 --- a/plugins/plugin.c +++ b/plugins/plugin.c @@ -46,6 +46,18 @@ void json(int client) { header(client,"application/json"); } +void textstream(int client) +{ + header(client, "text/event-stream"); +} +void octstream(int client, char* name) +{ + header_base(client); + __t(client,"Content-Type: application/octet-stream"); + __t(client,"Content-Disposition: attachment; filename=\"%s\"", name); + response(client,""); + //Content-Disposition: attachment; filename="fname.ext" +} void jpeg(int client) { header(client,"image/jpeg"); @@ -57,26 +69,28 @@ void header(int client, const char* type) response(client,""); } -void response(int client, const char* data) +int response(int client, const char* data) { char buf[BUFFLEN+3]; strcpy(buf, data); + int nbytes; int size = strlen(data); buf[size] = '\r'; buf[size+1] = '\n'; buf[size+2] = '\0'; - send(client, buf, strlen(buf), 0); + nbytes = send(client, buf, strlen(buf), 0); + return (nbytes ==-1?0:1); } -void __ti(int client,int data) +int __ti(int client,int data) { char str[15]; sprintf(str, "%d", data); - response(client,str); + return response(client,str); } -void __t(int client, const char* fstring,...) +int __t(int client, const char* fstring,...) { - + int nbytes; int dlen; int sent = 0; int buflen = 0; @@ -93,7 +107,7 @@ void __t(int client, const char* fstring,...) va_end(arguments); if(dlen < BUFFLEN) - response(client,data); + return response(client,data); else { while(sent < dlen - 1) @@ -108,23 +122,29 @@ void __t(int client, const char* fstring,...) //chunk[buflen-1] = '\0'; //response(client,chunk); sent += buflen; - send(client, chunk, buflen, 0); + nbytes = send(client, chunk, buflen, 0); free(chunk); + if(nbytes == -1) return 0; } chunk = "\r\n"; send(client, chunk, strlen(chunk), 0); } free(data); } + return 1; // } -void __b(int client, const unsigned char* data, int size) +int __b(int client, const unsigned char* data, int size) { char buf[BUFFLEN]; int sent = 0; int buflen = 0; + int nbytes; if(size <= BUFFLEN) - send(client,data,size,0); + { + nbytes = send(client,data,size,0); + return (nbytes==-1?0:1); + } else { while(sent < size) @@ -134,12 +154,14 @@ void __b(int client, const unsigned char* data, int size) else buflen = size - sent; memcpy(buf,data+sent,buflen); - send(client,buf,buflen,0); + nbytes = send(client,buf,buflen,0); sent += buflen; + if(nbytes == -1) return 0; } } + return 1; } -void __fb(int client, const char* file) +int __fb(int client, const char* file) { printf("Open file %s\n",file ); unsigned char buffer[BUFFLEN]; @@ -148,32 +170,37 @@ void __fb(int client, const char* file) if(!ptr) { LOG("Cannot read : %s\n", file); - return; + return 0; } + size_t size; while(!feof(ptr)) { - fread(buffer,BUFFLEN,1,ptr); - __b(client,buffer,BUFFLEN); + size = fread(buffer,1,BUFFLEN,ptr); + if(!__b(client,buffer,size)) return 0; } fclose(ptr); + return 1; } -void __f(int client, const char* file) +int __f(int client, const char* file) { unsigned char buf[BUFFLEN]; FILE *ptr; + int nbytes; ptr = fopen(file,"r"); if(!ptr) { LOG("Cannot read : %s\n", file); - return; + return 0; } fgets(buf, sizeof(buf), ptr); while(!feof(ptr)) { - send(client, buf, strlen(buf), 0); + nbytes = send(client, buf, strlen(buf), 0); + if(nbytes == -1) return 0; fgets(buf, sizeof(buf), ptr); } fclose(ptr); + return 1; } char* route(const char* repath) @@ -208,7 +235,7 @@ void set_cookie(int client,dictionary dic) __t(client,"Content-Type: text/html; charset=utf-8"); association assoc; for_each_assoc(assoc,dic){ - __t(client,"Set-Cookie: %s=%s",assoc->key, assoc->value.s); + __t(client,"Set-Cookie: %s=%s",assoc->key, (char*)assoc->value); } response(client,""); } \ No newline at end of file diff --git a/plugins/plugin.h b/plugins/plugin.h index d9ed097..8bf9f7f 100644 --- a/plugins/plugin.h +++ b/plugins/plugin.h @@ -8,10 +8,10 @@ #define IS_POST(method) (strcmp(method,"POST")== 0) #define IS_GET(method) (strcmp(method,"GET")== 0) -#define R_STR(d,k) (dvalue(d,k)) -#define R_INT(d,k) (dvalue_i(d,k)) -#define R_FLOAT(d,k) (dvalue_f(d,k)) -#define R_PTR(d,k) (dvalue_p(d,k)) +#define R_STR(d,k) ((char*)dvalue(d,k)) +#define R_INT(d,k) ((int)dvalue(d,k)) +#define R_FLOAT(d,k) ((double)dvalue(d,k)) +#define R_PTR(d,k) (dvalue(d,k)) #define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}" typedef struct { @@ -30,7 +30,7 @@ typedef sqlite3* sqldb; extern plugin_header __plugin__; extern call __init__; -void response(int, const char*); +int response(int, const char*); void header_base(int); void header(int,const char*); void redirect(int,const char*); @@ -38,11 +38,13 @@ void html(int); void text(int); void json(int); void jpeg(int); -void __ti(int,int); -void __t(int, const char*,...); -void __b(int, const unsigned char*, int); -void __f(int, const char*); -void __fb(int, const char*); +void octstream(int, char*); +void textstream(int); +int __ti(int,int); +int __t(int, const char*,...); +int __b(int, const unsigned char*, int); +int __f(int, const char*); +int __fb(int, const char*); int upload(const char*, const char*); char* route(const char*); char* htdocs(const char*); diff --git a/plugins/pluginsman/pluginsman.c.o b/plugins/pluginsman/pluginsman.c.o index 5c39932..bb80d10 100644 Binary files a/plugins/pluginsman/pluginsman.c.o and b/plugins/pluginsman/pluginsman.c.o differ diff --git a/plugins/utils.c b/plugins/utils.c index ee1a71c..469af6b 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -1,3 +1,26 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 "utils.h" char* __s(const char* fstring,...) { @@ -15,7 +38,7 @@ char* __s(const char* fstring,...) va_end(arguments); return data; } else - return NULL; + return ""; } @@ -296,3 +319,10 @@ char to_hex(char code) { static char hex[] = "0123456789abcdef"; return hex[code & 15]; } +unsigned hash(const char* key, int hash_size) +{ + unsigned hashval; + for (hashval = 0; *key != '\0'; key++) + hashval = *key + 31 * hashval; + return hashval % hash_size; +} diff --git a/plugins/utils.h b/plugins/utils.h index b922c80..e1d327f 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -1,3 +1,28 @@ +/* +The MIT License (MIT) + +Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com + +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 UTILS_H +#define UTILS_H #include #include #include @@ -24,8 +49,20 @@ #else #define LOG(a,...) do{}while(0) #endif -#define BUFFLEN 1024 - +#define BUFFLEN 1024 +#define HASHSIZE 1024 +#define DHASHSIZE 50 + +#define RPC_TYPE_ARRAY 601//hash("array") +#define RPC_TYPE_BASE64 335//hash("base64") +#define RPC_TYPE_BOOL 40//hash("boolean") +#define RPC_TYPE_DOUBLE 977//hash("double") +#define RPC_TYPE_DATE 49//hash("dateTime.iso8601") +#define RPC_TYPE_INT 1007//hash("int") +#define RPC_TYPE_I4 235//hash("i4") +#define RPC_TYPE_STRING 17//hash("string") +#define RPC_TYPE_NIL 529//hash("nil") + char* __s(const char*,...); void trim(char*,const char); void removeAll(const char* path,int mode); @@ -41,3 +78,5 @@ char *url_decode(const char *str); char *url_encode(const char *str); char from_hex(char ch); char to_hex(char code); +unsigned hash(const char*, int); +#endif