Browse Source
GET and POST vars are now parsed when request ist parsed. COOKIE will follow. While parsing the request line i also get pic the path from it.
master
GET and POST vars are now parsed when request ist parsed. COOKIE will follow. While parsing the request line i also get pic the path from it.
master
14 changed files with 393 additions and 30 deletions
-
8include/commons.h
-
20include/hash_value.h
-
29include/http/cookie.h
-
2include/http/parser.h
-
6include/http/request.h
-
7src/Makefile.am
-
87src/hash_value.c
-
82src/http/cookie.c
-
43src/http/parser/parse.c
-
49src/http/parser/post_vars.c
-
49src/http/parser/request_vars.c
-
8src/http/request.c
-
32src/http/worker/process.c
-
1src/server/poll.c
@ -0,0 +1,20 @@ |
|||
#ifndef __HASH_VALUE_H__ |
|||
#define __HASH_VALUE_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
CLASS(HashValue) { |
|||
unsigned long hash; |
|||
|
|||
char * key; |
|||
void * value; |
|||
|
|||
size_t nkey; |
|||
size_t nvalue; |
|||
}; |
|||
|
|||
#endif // __HASH_VALUE_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,29 @@ |
|||
#ifndef __HTTP_COOKIE_H__ |
|||
#define __HTTP_COOKIE_H__ |
|||
|
|||
#include <time.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
CLASS(HttpCookie) { |
|||
unsigned long hash; |
|||
|
|||
char * key; |
|||
char * value; |
|||
char * domain; |
|||
char * path; |
|||
|
|||
time_t expires; |
|||
time_t max_age; |
|||
|
|||
size_t nkey; |
|||
size_t nvalue; |
|||
}; |
|||
|
|||
char * httpCookieToString(HttpCookie); |
|||
HttpCookie httpStringToCookie(const char *); |
|||
|
|||
#endif // __HTTP_COOKIE_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,87 @@ |
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "hash_value.h" |
|||
#include "utils/hash.h" |
|||
#include "utils/memory.h" |
|||
#include "commons.h" |
|||
#include "interface/class.h" |
|||
#include "interface/hashable.h" |
|||
|
|||
static |
|||
int |
|||
hashValueCtor(void * _this, va_list * params) |
|||
{ |
|||
HashValue this = _this; |
|||
char * key = va_arg(* params, char*); |
|||
void * value; |
|||
|
|||
this->nkey = va_arg(* params, size_t); |
|||
value = va_arg(* params, void*); |
|||
this->nvalue = va_arg(* params, size_t); |
|||
|
|||
this->key = malloc(this->nkey + 1); |
|||
this->key[this->nkey] = 0; |
|||
memcpy(this->key, key, this->nkey); |
|||
|
|||
this->hash = sdbm((unsigned char *)this->key, this->nkey); |
|||
|
|||
if (NULL != value) { |
|||
this->value = malloc(this->nvalue + 1); |
|||
((char*)this->value)[this->nvalue] = 0; |
|||
memcpy(this->value, value, this->nvalue); |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
hashValueDtor(void * _this) |
|||
{ |
|||
HashValue this = _this; |
|||
|
|||
FREE(this->key); |
|||
FREE(this->value); |
|||
} |
|||
|
|||
static |
|||
unsigned long |
|||
hashValueGetHash(void * _this) |
|||
{ |
|||
HashValue this = _this; |
|||
|
|||
return this->hash; |
|||
} |
|||
|
|||
static |
|||
void |
|||
hashValueHandleDouble(void * _this, void * _double) |
|||
{ |
|||
HashValue this = _this; |
|||
HashValue doub = _double; |
|||
void * tmp_value; |
|||
size_t tmp_nvalue; |
|||
|
|||
/** |
|||
* here we swap the internal data of both objects, |
|||
* effectively overwriting the old entry. We need not |
|||
* to free anything here as _double will be deleted |
|||
* afterwards anyway (\see hash/add.c). |
|||
*/ |
|||
tmp_value = this->value; |
|||
this->value = doub->value; |
|||
doub->value = tmp_value; |
|||
|
|||
tmp_nvalue = this->nvalue; |
|||
this->nvalue = doub->nvalue; |
|||
doub->nvalue = tmp_nvalue; |
|||
} |
|||
|
|||
INIT_IFACE(Class, hashValueCtor, hashValueDtor, NULL); |
|||
INIT_IFACE(Hashable, hashValueGetHash, hashValueHandleDouble); |
|||
CREATE_CLASS(HashValue, NULL, IFACE(Class), IFACE(Hashable)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,82 @@ |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdarg.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "cookie.h" |
|||
#include "interface/class.h" |
|||
#include "interface/hashable" |
|||
|
|||
#include "utils/hash.h" |
|||
#include "utils/memory.h" |
|||
#include "commons.h" |
|||
|
|||
|
|||
static |
|||
int |
|||
httpCookieCtor(void * _this, va_list * params) |
|||
{ |
|||
HttpCookie this = _this; |
|||
char * key = va_arg(* params, char*); |
|||
char * value; |
|||
|
|||
this->nkey = va_arg(* params, size_t); |
|||
value = va_arg(* params, char*); |
|||
this->nvalue = va_arg(* params, size_t); |
|||
|
|||
this->key = malloc(this->nkey + 1); |
|||
this->key[this->nkey] = 0; |
|||
memcpy(this->key, key, this->nkey); |
|||
|
|||
this->value = malloc(this->nvalue + 1); |
|||
this->value[this->nvalue] = 0; |
|||
memcpy(this->value, value, this->nvalue); |
|||
|
|||
this->hash = sdbm((unsigned char *)key, nkey); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
httpCookieDtor(void * _this, va_list * params) |
|||
{ |
|||
HttpCookie this = _this; |
|||
|
|||
FREE(this->key); |
|||
FREE(this->value); |
|||
FREE(this->domain); |
|||
FREE(this->path); |
|||
} |
|||
|
|||
static |
|||
unsigned long |
|||
httpCookieGetHash(void * _this) |
|||
{ |
|||
HttpCookie this = _this; |
|||
|
|||
return this->hash; |
|||
} |
|||
|
|||
static |
|||
void |
|||
httpCookieHandleDouble(void * _this, void * _double) |
|||
{ |
|||
HttpCookie this = _this; |
|||
HttpCookie doub = _double; |
|||
|
|||
SWAP(char*, this->key, doub->key); |
|||
SWAP(char*, this->value, doub->value); |
|||
SWAP(char*, this->domain, doub->domain); |
|||
SWAP(char*, this->path, doub->path); |
|||
|
|||
SWAP(char*, this->nkey, doub->nkey); |
|||
SWAP(char*, this->nvalue, doub->nvalue); |
|||
} |
|||
|
|||
|
|||
INIT_IFACE(Class, httpCookieCtor, httpCookieDtor, NULL); |
|||
INIT_IFACE(Hashable, httpCookieGetHash, httpCookieHandleDouble); |
|||
CREATE_CLASS(HttpCookie, NULL, IFACE(Class), IFACE(Hashable)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,49 @@ |
|||
#include <string.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "http/parser.h" |
|||
#include "http/request.h" |
|||
#include "hash_value.h" |
|||
#include "hash.h" |
|||
#include "interface/class.h" |
|||
|
|||
/** |
|||
* \todo this is very similar to other pair parsing |
|||
* things... key1=val1<delim>key2=val2<delim>...keyn=valn |
|||
* Generalize this!!!! |
|||
*/ |
|||
void |
|||
httpParserPostVars(HttpParser this) |
|||
{ |
|||
HttpRequest request = (HttpRequest)this->current; |
|||
char * pair = this->current->body; |
|||
size_t togo = this->current->nbody; |
|||
|
|||
while(NULL != pair && 0 < togo) { |
|||
char * key = pair; |
|||
char * eqsign = memchr(key, '=', togo); |
|||
char * value; |
|||
size_t nvalue; |
|||
|
|||
if (NULL == eqsign) { |
|||
return; |
|||
} |
|||
|
|||
togo -= (eqsign - key); |
|||
pair = memchr(eqsign, '&', togo); |
|||
|
|||
if (NULL == pair) { |
|||
pair = &(this->current->body[this->current->nbody]); |
|||
} |
|||
|
|||
nvalue = pair-eqsign-1; |
|||
value = (0 != nvalue)? eqsign+1 : NULL; |
|||
|
|||
hashAdd(request->post, |
|||
new(HashValue, key, eqsign-key, value, nvalue)); |
|||
|
|||
togo -= (pair - eqsign); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,49 @@ |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "http/parser.h" |
|||
#include "http/request.h" |
|||
#include "hash_value.h" |
|||
#include "hash.h" |
|||
#include "interface/class.h" |
|||
|
|||
void |
|||
httpParserRequestVars(HttpParser this) |
|||
{ |
|||
HttpRequest request = (HttpRequest)this->current; |
|||
char * delim = strchr(request->uri, '?'); |
|||
|
|||
if (NULL == delim) { |
|||
delim = request->uri + strlen(request->uri); |
|||
} |
|||
|
|||
request->path = malloc(delim - request->uri + 1); |
|||
request->path[delim - request->uri + 1] = 0; |
|||
memcpy(request->path, request->uri, delim - request->uri + 1); |
|||
|
|||
while(NULL != delim && 0 != *delim) { |
|||
char * key = delim + 1; |
|||
char * eqsign = strchr(key, '='); |
|||
char * value; |
|||
size_t nvalue; |
|||
|
|||
if (NULL == eqsign) { |
|||
return; |
|||
} |
|||
|
|||
delim = strchr(eqsign, '&'); |
|||
|
|||
if (NULL == delim) { |
|||
delim = key + strlen(key); |
|||
} |
|||
|
|||
nvalue = delim-eqsign-1; |
|||
value = (0 != nvalue)? eqsign+1 : NULL; |
|||
|
|||
hashAdd(request->get, |
|||
new(HashValue, key, eqsign-key, value, nvalue)); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue