Fix bugs + inprovement

This commit is contained in:
lxsang 2015-11-24 17:58:32 +01:00
parent 84c8875588
commit 27fd4a0134
19 changed files with 452 additions and 302 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.[oa]

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,7 +9,7 @@ void accept_request(int client)
char buf[1024]; char buf[1024];
int numchars; int numchars;
char method[255]; char method[255];
char url[255]; char url[4096];
char path[512]; char path[512];
size_t i, j; size_t i, j;
struct stat st; struct stat st;

View File

@ -349,7 +349,7 @@ int read_buf(int sock, char*buf,int size)
struct plugin_entry *plugin_lookup(char *s) struct plugin_entry *plugin_lookup(char *s)
{ {
struct plugin_entry *np; 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) if (strcmp(s, np->pname) == 0)
return np; /* found */ return np; /* found */
return NULL; /* not found */ return NULL; /* not found */
@ -371,7 +371,7 @@ struct plugin_entry *plugin_load(char *name)
return NULL; return NULL;
if ((np->handle = plugin_from_file(name)) == NULL) if ((np->handle = plugin_from_file(name)) == NULL)
return NULL; return NULL;
hashval = hash(name); hashval = hash(name,HASHSIZE);
np->next = plugin_table[hashval]; np->next = plugin_table[hashval];
plugin_table[hashval] = np; plugin_table[hashval] = np;
} else /* already there */ } else /* already there */

View File

@ -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" #include "dictionary.h"
dictionary dict() dictionary dict()
@ -7,18 +30,11 @@ dictionary dict()
d[i] = NULL; d[i] = NULL;
return d; 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 dlookup(dictionary dic,const char* key)
{ {
association np; association np;
if(dic == NULL) return NULL; 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) if(!np || !np->key)
{ {
@ -38,55 +54,23 @@ association __put_el_with_key(dictionary dic, const char* key)
np = (association) malloc(sizeof(*np)); np = (association) malloc(sizeof(*np));
if (np == NULL || (np->key = strdup(key)) == NULL) if (np == NULL || (np->key = strdup(key)) == NULL)
return NULL; return NULL;
hashval = hash(key); hashval = hash(key, DHASHSIZE);
np->next = dic[hashval]; np->next = dic[hashval];
dic[hashval] = np; dic[hashval] = np;
} }
return np; return np;
} }
association dput(dictionary dic,const char* key, const char* value) association dput(dictionary dic,const char* key, void* 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 np = __put_el_with_key(dic,key); association np = __put_el_with_key(dic,key);
if(np == NULL) return NULL; if(np == NULL) return NULL;
if(value == NULL) np->value.s=""; np->value = value;
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;
return np; return np;
} }
int dremove(dictionary dic, const char* key) int dremove(dictionary dic, const char* key)
{ {
if(dic == NULL) return 0; if(dic == NULL) return 0;
int hashval = hash(key); int hashval = hash(key, DHASHSIZE);
association np = dic[hashval]; association np = dic[hashval];
if(np!=NULL && strcmp(key,np->key)==0) if(np!=NULL && strcmp(key,np->key)==0)
{ {
@ -102,36 +86,11 @@ int dremove(dictionary dic, const char* key)
return 0; /* not found */ return 0; /* not found */
} }
/** void* dvalue(dictionary dic, const char* key)
* Get the string data by key
* @param dic the dictionary
* @param key @
* @return the string value
*/
char* dvalue(dictionary dic,const char* key)
{ {
association as = dlookup(dic,key); association as = dlookup(dic,key);
if(as == NULL) return NULL; if(as == NULL) return NULL;
return as->value.s; return as->value;
}
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;
} }
void freedict(dictionary dic){free(dic);} void freedict(dictionary dic){free(dic);}

View File

@ -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" #include "utils.h"
#define HASHSIZE 255
#define for_each_assoc(assoc, dic) \ #define for_each_assoc(assoc, dic) \
for(int i = 0; i < HASHSIZE; i++) \ for(int i = 0; i < HASHSIZE; i++) \
for(assoc = dic[i];assoc!= NULL; assoc = assoc->next) for(assoc = dic[i];assoc!= NULL; assoc = assoc->next)
@ -10,29 +35,17 @@
typedef struct __assoc{ typedef struct __assoc{
struct __assoc *next; struct __assoc *next;
char *key; char *key;
union void* value;
{
int i;
char* s;
float f;
void* p;
} value;
//char *value; //char *value;
} * association; } * association;
typedef association* dictionary; typedef association* dictionary;
dictionary dict(); dictionary dict();
unsigned hash(const char*);
association dlookup(dictionary,const char*); association dlookup(dictionary,const char*);
char* dvalue(dictionary, const char*); void* dvalue(dictionary, const char*);
int dvalue_i(dictionary, const char*); association dput(dictionary,const char*, void*);
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*);
int dremove(dictionary, const char*); int dremove(dictionary, const char*);
void freedict(dictionary); void freedict(dictionary);
void stest(const char* ); void stest(const char* );
#endif

View File

@ -15,11 +15,11 @@ char* folder_list_from(const char* aPath)
{ {
for(list np = path; np != NULL; np=np->next) 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) if(flist == NULL)
flist = __s(FOLLIST, np->e.value.s,route); flist = __s(FOLLIST, np->value.s,route);
else 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); free(path);

Binary file not shown.

View File

@ -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" #include "list.h"
Atom __atom() list list_init()
{ {
Atom ret = (Atom){.type = T_UNDEF}; list ret = (list)malloc(sizeof *ret);
return ret; ret->type = RPC_TYPE_NIL;
}
Atom atom(int type)
{
Atom ret = (Atom){.type = type};
return ret;
}
list __list()
{
list ret = malloc(sizeof *ret);
ret->e.type = T_UNDEF;
ret->next = NULL; ret->next = NULL;
return ret; return ret;
} }
void list_put(list* l, item it)
list last(list l)
{ {
list p = l; if(*l == NULL || (*l)->type == RPC_TYPE_NIL)
while( p && p->next != NULL) {
*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; p = p->next;
return p; return p;
} }
int list_remove(list l,int idx)
int size(list l)
{ {
int i = 0; if(l==NULL) return 0;
list np = l; 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) while(np)
{ {
np = np->next; np = np->next;
@ -37,128 +93,147 @@ int size(list l)
} }
return i; return i;
} }
Atom lvalueAt(list l,int idx) char* as_string(list l)
{ {
list np = lat(l,idx); char* str = "";
if(np == NULL) return __atom(); if(l != NULL && l->type != RPC_TYPE_NIL)
return np->e;
}
list lat(list l,int idx)
{
int i = 0;
list np = l;
while(np && np->next != NULL)
{ {
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 = "<Unknown>";
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; return np;
np = np->next; np = np->next;
i++; i++;
} }
return NULL; return NULL;
} }
list lput(list* l, Atom a) item list_item(int type)
{ {
list new_item = malloc(sizeof *new_item); item ret = (item)malloc(sizeof *ret);
new_item->e = a; ret->type = type;
new_item->next = NULL; ret->next = NULL;
if((*l) == NULL || (*l)->e.type == T_UNDEF) return ret;
{
(*l) = new_item;
return (*l);
}
list np = last(*l);
np->next = new_item;
return np->next;
} }
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) list split(const char* str, const char* delim)
{ {
if(str == NULL || delim == NULL) return NULL; if(str == NULL || delim == NULL) return NULL;
char* str_cpy = strdup(str); char* str_cpy = strdup(str);
char* token; char* token;
Atom v = atom(T_STR); list l = list_init();
list l = __list();
while((token = strsep(&str_cpy,delim))) while((token = strsep(&str_cpy,delim)))
{ {
if(strlen(token) > 0) if(strlen(token) > 0)
{ {
v = (Atom){.value.s = strdup(token)}; list_put_special(&l,token);
lput(&l,v);
} }
} }
free(str_cpy); free(str_cpy);
if(l->e.type== T_UNDEF) if(l->type== RPC_TYPE_NIL)
return NULL; return NULL;
return l; 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;
}

View File

@ -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" #include "utils.h"
#define list item
#define T_STR 0 typedef struct __item{
#define T_INT 1 int type;
#define T_FLOAT 2 union{
#define T_FIELD 3 int i;
#define T_UNDEF 4 int b;
char* s;
double d;
typedef struct char* date;
{ char* b64;
char* name; struct __item* array;
int type;
union
{
int i;
float f;
char* s;
} value; } value;
} field; struct __item* next;
}*item;
typedef struct list list_init();
{ void list_put(list*,item);
int type; void list_put_i(list*,int);
union void list_put_d(list*,double);
{ void list_put_b(list*,int);
int i; void list_put_b64(list*,const char*);
float f; void list_put_date(list*,const char*);
char* s; void list_put_s(list*,const char*);
field fi; void list_put_array(list*,list);
} value; item list_last(list);
} Atom; int list_remove(list,int);
int list_size(list);
typedef struct _item item list_at(list,int);
{ int list_empty(list);
Atom e; item list_item(int type);
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 split(const char*, const char*); list split(const char*, const char*);
char* as_string(list);
void list_put_special(list*, const char*);
void list_free(list *);
#endif

View File

@ -46,6 +46,18 @@ void json(int client)
{ {
header(client,"application/json"); 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) void jpeg(int client)
{ {
header(client,"image/jpeg"); header(client,"image/jpeg");
@ -57,26 +69,28 @@ void header(int client, const char* type)
response(client,""); response(client,"");
} }
void response(int client, const char* data) int response(int client, const char* data)
{ {
char buf[BUFFLEN+3]; char buf[BUFFLEN+3];
strcpy(buf, data); strcpy(buf, data);
int nbytes;
int size = strlen(data); int size = strlen(data);
buf[size] = '\r'; buf[size] = '\r';
buf[size+1] = '\n'; buf[size+1] = '\n';
buf[size+2] = '\0'; 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]; char str[15];
sprintf(str, "%d", data); 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 dlen;
int sent = 0; int sent = 0;
int buflen = 0; int buflen = 0;
@ -93,7 +107,7 @@ void __t(int client, const char* fstring,...)
va_end(arguments); va_end(arguments);
if(dlen < BUFFLEN) if(dlen < BUFFLEN)
response(client,data); return response(client,data);
else else
{ {
while(sent < dlen - 1) while(sent < dlen - 1)
@ -108,23 +122,29 @@ void __t(int client, const char* fstring,...)
//chunk[buflen-1] = '\0'; //chunk[buflen-1] = '\0';
//response(client,chunk); //response(client,chunk);
sent += buflen; sent += buflen;
send(client, chunk, buflen, 0); nbytes = send(client, chunk, buflen, 0);
free(chunk); free(chunk);
if(nbytes == -1) return 0;
} }
chunk = "\r\n"; chunk = "\r\n";
send(client, chunk, strlen(chunk), 0); send(client, chunk, strlen(chunk), 0);
} }
free(data); 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]; char buf[BUFFLEN];
int sent = 0; int sent = 0;
int buflen = 0; int buflen = 0;
int nbytes;
if(size <= BUFFLEN) if(size <= BUFFLEN)
send(client,data,size,0); {
nbytes = send(client,data,size,0);
return (nbytes==-1?0:1);
}
else else
{ {
while(sent < size) while(sent < size)
@ -134,12 +154,14 @@ void __b(int client, const unsigned char* data, int size)
else else
buflen = size - sent; buflen = size - sent;
memcpy(buf,data+sent,buflen); memcpy(buf,data+sent,buflen);
send(client,buf,buflen,0); nbytes = send(client,buf,buflen,0);
sent += buflen; 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 ); printf("Open file %s\n",file );
unsigned char buffer[BUFFLEN]; unsigned char buffer[BUFFLEN];
@ -148,32 +170,37 @@ void __fb(int client, const char* file)
if(!ptr) if(!ptr)
{ {
LOG("Cannot read : %s\n", file); LOG("Cannot read : %s\n", file);
return; return 0;
} }
size_t size;
while(!feof(ptr)) while(!feof(ptr))
{ {
fread(buffer,BUFFLEN,1,ptr); size = fread(buffer,1,BUFFLEN,ptr);
__b(client,buffer,BUFFLEN); if(!__b(client,buffer,size)) return 0;
} }
fclose(ptr); fclose(ptr);
return 1;
} }
void __f(int client, const char* file) int __f(int client, const char* file)
{ {
unsigned char buf[BUFFLEN]; unsigned char buf[BUFFLEN];
FILE *ptr; FILE *ptr;
int nbytes;
ptr = fopen(file,"r"); ptr = fopen(file,"r");
if(!ptr) if(!ptr)
{ {
LOG("Cannot read : %s\n", file); LOG("Cannot read : %s\n", file);
return; return 0;
} }
fgets(buf, sizeof(buf), ptr); fgets(buf, sizeof(buf), ptr);
while(!feof(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); fgets(buf, sizeof(buf), ptr);
} }
fclose(ptr); fclose(ptr);
return 1;
} }
char* route(const char* repath) 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"); __t(client,"Content-Type: text/html; charset=utf-8");
association assoc; association assoc;
for_each_assoc(assoc,dic){ 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,""); response(client,"");
} }

View File

@ -8,10 +8,10 @@
#define IS_POST(method) (strcmp(method,"POST")== 0) #define IS_POST(method) (strcmp(method,"POST")== 0)
#define IS_GET(method) (strcmp(method,"GET")== 0) #define IS_GET(method) (strcmp(method,"GET")== 0)
#define R_STR(d,k) (dvalue(d,k)) #define R_STR(d,k) ((char*)dvalue(d,k))
#define R_INT(d,k) (dvalue_i(d,k)) #define R_INT(d,k) ((int)dvalue(d,k))
#define R_FLOAT(d,k) (dvalue_f(d,k)) #define R_FLOAT(d,k) ((double)dvalue(d,k))
#define R_PTR(d,k) (dvalue_p(d,k)) #define R_PTR(d,k) (dvalue(d,k))
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}" #define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
typedef struct { typedef struct {
@ -30,7 +30,7 @@ typedef sqlite3* sqldb;
extern plugin_header __plugin__; extern plugin_header __plugin__;
extern call __init__; extern call __init__;
void response(int, const char*); int response(int, const char*);
void header_base(int); void header_base(int);
void header(int,const char*); void header(int,const char*);
void redirect(int,const char*); void redirect(int,const char*);
@ -38,11 +38,13 @@ void html(int);
void text(int); void text(int);
void json(int); void json(int);
void jpeg(int); void jpeg(int);
void __ti(int,int); void octstream(int, char*);
void __t(int, const char*,...); void textstream(int);
void __b(int, const unsigned char*, int); int __ti(int,int);
void __f(int, const char*); int __t(int, const char*,...);
void __fb(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*); int upload(const char*, const char*);
char* route(const char*); char* route(const char*);
char* htdocs(const char*); char* htdocs(const char*);

Binary file not shown.

View File

@ -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" #include "utils.h"
char* __s(const char* fstring,...) char* __s(const char* fstring,...)
{ {
@ -15,7 +38,7 @@ char* __s(const char* fstring,...)
va_end(arguments); va_end(arguments);
return data; return data;
} else } else
return NULL; return "";
} }
@ -296,3 +319,10 @@ char to_hex(char code) {
static char hex[] = "0123456789abcdef"; static char hex[] = "0123456789abcdef";
return hex[code & 15]; 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;
}

View File

@ -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 <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h> #include <unistd.h>
@ -24,8 +49,20 @@
#else #else
#define LOG(a,...) do{}while(0) #define LOG(a,...) do{}while(0)
#endif #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*,...); char* __s(const char*,...);
void trim(char*,const char); void trim(char*,const char);
void removeAll(const char* path,int mode); void removeAll(const char* path,int mode);
@ -41,3 +78,5 @@ char *url_decode(const char *str);
char *url_encode(const char *str); char *url_encode(const char *str);
char from_hex(char ch); char from_hex(char ch);
char to_hex(char code); char to_hex(char code);
unsigned hash(const char*, int);
#endif