taskrambler  v0.1.8
Web server and task management solution.
http/parser/parse.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 
25 #include "class.h"
26 #include "cbuf.h"
27 #include "stream.h"
28 #include "queue.h"
29 
30 #include "http/parser.h"
31 #include "http/header.h"
33 
34 #include "utils/memory.h"
35 #include "commons.h"
36 
37 #define MIN(a,b) ((a)<(b)? (a) : (b))
38 
39 
40 ssize_t
41 httpParserParse(void * _this, Stream st)
42 {
43  HttpParser this = _this;
44  int cont = 1;
45  ssize_t read;
46  char * line;
47  char * line_end;
48 
49  if (cbufIsLocked(this->buffer)) {
50  if (FALSE == this->ourLock)
51  return 0;
52  }
53  else {
54  cbufLock(this->buffer);
55  this->ourLock = TRUE;
56  }
57 
58  if (NULL != this->incomplete) {
59  cbufSetData(this->buffer, this->incomplete, this->isize);
60  MEM_FREE(this->incomplete);
61  }
62 
63  if (0 > (read = cbufRead(this->buffer, st))) {
64  cbufRelease(this->buffer);
65  this->ourLock = FALSE;
66  return read;
67  }
68 
69  while (cont) {
70  switch(this->state) {
72  cbufSkipNonAlpha(this->buffer);
73  if (! cbufIsEmpty(this->buffer)) {
74  this->state = HTTP_MESSAGE_START;
75  }
76  else {
77  cbufRelease(this->buffer);
78  this->ourLock = FALSE;
79  cont = 0;
80  break;
81  }
82 
83  case HTTP_MESSAGE_START:
84  if (NULL == (line = cbufGetLine(this->buffer, &line_end))) {
85  if (! cbufIsEmpty(this->buffer)) {
86  this->isize = this->buffer->bused;
87  this->incomplete = memMalloc(this->isize);
88  memcpy(this->incomplete,
89  cbufGetData(this->buffer, this->isize),
90  this->isize);
91  }
92  cbufRelease(this->buffer);
93  this->ourLock = FALSE;
94  cont = 0;
95  break;
96  }
97 
98  httpParserNewMessage(this, line, line_end);
99  if (NULL == this->current) {
100  cbufRelease(this->buffer);
101  this->ourLock = FALSE;
102  return -2; // a server error occured can't process...
103  }
104  httpParserRequestVars(this);
105 
106  this->state = HTTP_MESSAGE_INTRO_DONE;
107 
109  if (NULL == (line = cbufGetLine(this->buffer, &line_end))) {
110  if (! cbufIsEmpty(this->buffer)) {
111  this->isize = this->buffer->bused;
112  this->incomplete = memMalloc(this->isize);
113  memcpy(this->incomplete,
114  cbufGetData(this->buffer, this->isize),
115  this->isize);
116  }
117  cbufRelease(this->buffer);
118  this->ourLock = FALSE;
119  cont = 0;
120  break;
121  }
122 
123  if (0 != strlen(line)) {
124  httpParserHeader(this, line, line_end);
125  break;
126  }
127 
128  this->state = HTTP_MESSAGE_HEADERS_DONE;
129 
131  if (this->current->dbody == this->current->nbody) {
132  this->state = HTTP_MESSAGE_DONE;
133  } else {
134  if (cbufIsEmpty(this->buffer)) {
135  cbufRelease(this->buffer);
136  this->ourLock = FALSE;
137  cont = 0;
138  break;
139  }
140 
141  cbufIncRead(
142  this->buffer,
144  this,
145  cbufGetRead(this->buffer),
146  this->buffer->bused));
147 
148  break;
149  }
150 
151  case HTTP_MESSAGE_DONE:
152  {
153  HttpHeader enc = hashGet(
154  this->current->header,
155  CSTRA("content-type"));
156 
157  /**
158  * do we have form data??
159  */
160  if (NULL != enc && 0 == strncasecmp(
161  "application/x-www-form-urlencoded",
162  enc->value[0],
163  MIN(sizeof("application/x-www-form-urlencoded")-1,
164  enc->nvalue[0]))) {
165  //!> then parse them...
166  httpParserPostVars(this);
167  }
168 
169  /**
170  * enqueue current request
171  */
172  queuePut(this->queue, this->current);
173  this->current = NULL;
174 
175  /**
176  * prepare for next request
177  */
178  this->state = HTTP_MESSAGE_GARBAGE;
179  }
180  break;
181 
182  default:
183  break;
184  }
185  }
186 
187  return this->queue->nmsg;
188 }
189 
190 // vim: set ts=4 sw=4:
char cbufIsEmpty(Cbuf this)
Definition: is_empty.c:26
#define MEM_FREE(seg)
Definition: memory.h:28
char * cbufGetData(Cbuf, size_t)
Definition: get_data.c:29
void httpParserRequestVars(HttpParser)
char * cbufSetData(Cbuf, const void *, size_t)
Definition: set_data.c:30
void cbufLock(Cbuf this)
Definition: lock.c:26
void cbufRelease(Cbuf this)
Definition: release.c:26
void * memMalloc(size_t)
Definition: memory.c:783
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
Bool cbufIsLocked(Cbuf this)
Definition: is_locked.c:28
#define FALSE
Definition: commons.h:28
void * hashGet(Hash, const char *, size_t)
Definition: hash/get.c:51
void queuePut(Queue, void *)
Definition: queue/put.c:27
#define TRUE
Definition: commons.h:27
void httpParserNewMessage(HttpParser, const char *, const char *lend)
Definition: new_message.c:28
ssize_t cbufRead(Cbuf, Stream)
Definition: cbuf/read.c:32
char * cbufGetLine(Cbuf, char **)
Definition: get_line.c:30
#define MIN(a, b)
void httpParserHeader(HttpParser, const char *, const char *)
Definition: p_header.c:37
void httpParserPostVars(HttpParser)
Definition: p_post_vars.c:39
ssize_t httpParserParse(void *_this, Stream st)
size_t httpParserBody(HttpParser, const char *, size_t)
Definition: p_body.c:34
void cbufIncRead(Cbuf this, size_t n)
Definition: inc_read.c:28
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26
void cbufSkipNonAlpha(Cbuf this)