From cef76c0a0932ebf036f7334aa9c2d21410ccbfed Mon Sep 17 00:00:00 2001 From: snowdrop Date: Wed, 21 Jan 2004 12:28:20 +0000 Subject: added server functionality --- nanohttp/nanohttp-common.c | 151 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) (limited to 'nanohttp/nanohttp-common.c') diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index 29c237a..55a9f7f 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.c,v 1.6 2003/12/18 15:32:09 snowdrop Exp $ + * $Id: nanohttp-common.c,v 1.7 2004/01/21 12:28:20 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -419,6 +419,155 @@ void hurl_free(hurl_t *url) } +/* request stuff */ + +/* ----------------------------------------------------- + FUNCTION: hrequest_new_from_buffer +----------------------------------------------------- */ +hrequest_t *hrequest_new_from_buffer(char *data) +{ + hrequest_t *req; + hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; + + char *tmp; + char *tmp2; + char *saveptr; + char *saveptr2; + char *saveptr3; + char *result; + char *key; + char *value; + char *opt_key; + char *opt_value; + int firstline = 1; + + req = (hrequest_t*)malloc(sizeof(hrequest_t)); + + req->method = NULL; + req->spec = NULL; + req->path = NULL; + req->query = NULL; + req->header = NULL; + + tmp = data; + + for (;;) { + result = (char*)strtok_r(tmp, "\n", &saveptr); + tmp=saveptr; + + if (result == NULL) + break; + + if (firstline) { + firstline=0; + tmp2 = result; + + /* parse [GET|POST] [PATH] [SPEC] */ + key = (char*)strtok_r(tmp2," ", &saveptr2); + + /* save method (get or post) */ + tmp2 = saveptr2; + if (key != NULL) { + req->method = (char*)malloc(strlen(key)+1); + strcpy(req->method, key); + } + + /* below is key the path and tmp2 the spec */ + key = (char*)strtok_r(tmp2," ", &saveptr2); + + /* save spec */ + tmp2 = saveptr2; + if (tmp2 != NULL) { + req->spec = (char*)malloc(strlen(tmp2)+1); + strcpy(req->spec, tmp2); + } + + /* parse and save path+query + parse: /path/of/target?key1=value1&key2=value2... + */ + + if (key != NULL) { + tmp2 = key; + key = (char*)strtok_r(tmp2,"?", &saveptr2); + tmp2 = saveptr2; + + /* save path */ + req->path = (char*)malloc(strlen(key)+1); + strcpy(req->path, key); + + /* parse options */ + for (;;) { + key = (char*)strtok_r(tmp2,"&",&saveptr2); + tmp2 = saveptr2; + + if (key == NULL) + break; + + opt_key = (char*)strtok_r(key,"=", &saveptr3); + opt_value = saveptr3; + + if (opt_value == NULL) + opt_value = ""; + + /* create option pair */ + if (opt_key != NULL) { + tmppair = (hpair_t*)malloc(sizeof(hpair_t)); + + if (req->query == NULL) { + req->query = qpair = tmppair; + } else { + qpair->next = tmppair; + qpair = tmppair; + } + + /* fill hpairnode_t struct */ + qpair->next = NULL; + qpair->key = (char*)malloc(strlen(opt_key)+1); + qpair->value = (char*)malloc(strlen(opt_value)+1); + + strcpy(qpair->key, opt_key); + strcpy(qpair->value, opt_value); + + } + } + } + + } else { + + /* parse "key: value" */ + tmp2 = result; + key = (char*)strtok_r(tmp2, ": ", &saveptr2); + value = saveptr2; + + /* create pair */ + tmppair = (hpair_t*)malloc(sizeof(hpair_t)); + + if (req->header == NULL) { + req->header = hpair = tmppair; + } else { + hpair->next = tmppair; + hpair = tmppair; + } + + /* fill pairnode_t struct */ + hpair->next = NULL; + hpair->key = (char*)malloc(strlen(key)+1); + hpair->value = (char*)malloc(strlen(value)+1); + + strcpy(hpair->key, key); + strcpy(hpair->value, value); + } + } + + return req; +} + + +void hrequest_free(hrequest_t *req) +{ + log_warn1("hrequest_free() not implemented!"); +} + /* response stuff */ -- cgit v1.1-32-gdbae