taskrambler  0.1.9
Web server and task management solution.
request.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 
28 #include "class.h"
29 #include "hash.h"
31 
32 #include "http/request.h"
33 #include "utils/memory.h"
34 
35 
36 static
37 int
38 httpRequestCtor(void * _this, va_list * params)
39 {
40  HttpRequest this = _this;
41  char * method, * uri;
42  size_t mlen, ulen;
43 
44  method = va_arg(* params, char *);
45  mlen = va_arg(* params, size_t);
46  uri = va_arg(* params, char *);
47  ulen = va_arg(* params, size_t);
48 
49  PARENTCALL(_this, Class, ctor, params);
50 
51  this->method = memMalloc(mlen + 1);
52  this->method[mlen] = 0;
53  memcpy(this->method, method, mlen);
54 
55  this->uri = memMalloc(ulen + 1);
56  this->uri[ulen] = 0;
57  memcpy(this->uri, uri, ulen);
58 
59  this->method_id = httpRequestGetMethodId(this);
60 
61  if (-1 == this->method_id) {
62  MEM_FREE(this->uri);
63  MEM_FREE(this->method);
64  MEM_FREE(this->path); /** \todo looks like path is not used at all */
65 
66  PARENTCALL(_this, Class, dtor);
67 
68  return -1;
69  }
70 
71  this->get = new(Hash);
72  this->post = new(Hash);
73  this->cookies = new(Hash);
74 
75  return 0;
76 }
77 
78 static
79 void
80 httpRequestDtor(void * _this)
81 {
82  HttpRequest this = _this;
83 
84  delete(this->get);
85  delete(this->post);
86  delete(this->cookies);
87 
88  MEM_FREE(this->uri);
89  MEM_FREE(this->method);
90  MEM_FREE(this->path);
91 
92  PARENTCALL(_this, Class, dtor);
93 }
94 
95 static
96 size_t
97 sizeGet(void * _this)
98 {
99  HttpRequest this = _this;
100  size_t size = 0;
101 
102  size += strlen(this->method) + 1;
103  size += strlen(this->uri) + 1;
104  size += strlen(((HttpMessage)this)->version) + 2;
105 
106  return size;
107 }
108 
109 static
110 char *
111 toString(void * _this, char * string)
112 {
113  HttpRequest this = _this;
114 
115  strcpy(string, this->method);
116  string += strlen(string);
117  *string++ = ' ';
118 
119  strcpy(string, this->uri);
120  string += strlen(string);
121  *string++ = ' ';
122 
123  strcpy(string, ((HttpMessage)this)->version);
124  string += strlen(string);
125  *string++ = '\r';
126  *string++ = '\n';
127 
128  return string;
129 }
130 
132 INIT_IFACE(HttpIntro, sizeGet, toString);
133 CREATE_CLASS(HttpRequest,
134  HttpMessage,
135  IFACE(Class),
136  IFACE(HttpIntro));
137 
138 // vim: set ts=4 sw=4:
void post(struct element *tree, void(*cb)(struct element *, int))
Definition: rbtree.c:618
#define MEM_FREE(seg)
Definition: memory.h:28
HttpMethod httpRequestGetMethodId(HttpRequest)
Definition: get_method_id.c:39
static int httpRequestCtor(void *_this, va_list *params)
Definition: request.c:38
void * memMalloc(size_t)
Definition: memory.c:783
#define IFACE(name)
Definition: interface.h:34
static size_t size
static char * string
static char * toString(void *_this, char *string)
Definition: request.c:111
static size_t sizeGet(void *_this)
Definition: request.c:97
static void httpRequestDtor(void *_this)
Definition: request.c:80
INIT_IFACE(Class, httpRequestCtor, httpRequestDtor, NULL)
CREATE_CLASS(HttpRequest, HttpMessage, IFACE(Class), IFACE(HttpIntro))
#define PARENTCALL(object, _iface, method,...)
Definition: class/class.h:120