|
Server 0.0.1
HTTP/REST server implementation
|
00001 00023 #include <stdlib.h> 00024 #include <stdarg.h> 00025 #include <string.h> 00026 #include <sys/types.h> 00027 00028 #include "class.h" 00029 #include "hash.h" 00030 #include "http/interface/http_intro.h" 00031 00032 #include "http/request.h" 00033 #include "utils/memory.h" 00034 00035 00036 static 00037 int 00038 httpRequestCtor(void * _this, va_list * params) 00039 { 00040 HttpRequest this = _this; 00041 char * method, * uri; 00042 size_t mlen, ulen; 00043 00044 method = va_arg(* params, char *); 00045 mlen = va_arg(* params, size_t); 00046 uri = va_arg(* params, char *); 00047 ulen = va_arg(* params, size_t); 00048 00049 PARENTCALL(_this, Class, ctor, params); 00050 00051 this->method = malloc(mlen + 1); 00052 this->method[mlen] = 0; 00053 memcpy(this->method, method, mlen); 00054 00055 this->uri = malloc(ulen + 1); 00056 this->uri[ulen] = 0; 00057 memcpy(this->uri, uri, ulen); 00058 00059 this->get = new(Hash); 00060 this->post = new(Hash); 00061 this->cookies = new(Hash); 00062 00063 return 0; 00064 } 00065 00066 static 00067 void 00068 httpRequestDtor(void * _this) 00069 { 00070 HttpRequest this = _this; 00071 00072 delete(this->get); 00073 delete(this->post); 00074 delete(this->cookies); 00075 00076 FREE(this->uri); 00077 FREE(this->method); 00078 FREE(this->path); 00079 00080 PARENTCALL(_this, Class, dtor); 00081 } 00082 00083 static 00084 size_t 00085 sizeGet(void * _this) 00086 { 00087 HttpRequest this = _this; 00088 size_t size = 0; 00089 00090 size += strlen(this->method) + 1; 00091 size += strlen(this->uri) + 1; 00092 size += strlen(((HttpMessage)this)->version) + 2; 00093 00094 return size; 00095 } 00096 00097 static 00098 char * 00099 toString(void * _this, char * string) 00100 { 00101 HttpRequest this = _this; 00102 00103 strcpy(string, this->method); 00104 string += strlen(string); 00105 *string++ = ' '; 00106 00107 strcpy(string, this->uri); 00108 string += strlen(string); 00109 *string++ = ' '; 00110 00111 strcpy(string, ((HttpMessage)this)->version); 00112 string += strlen(string); 00113 *string++ = '\r'; 00114 *string++ = '\n'; 00115 00116 return string; 00117 } 00118 00119 INIT_IFACE(Class, httpRequestCtor, httpRequestDtor, NULL); 00120 INIT_IFACE(HttpIntro, sizeGet, toString); 00121 CREATE_CLASS(HttpRequest, 00122 HttpMessage, 00123 IFACE(Class), 00124 IFACE(HttpIntro)); 00125 00126 // vim: set ts=4 sw=4: