taskrambler  0.1.9
Web server and task management solution.
http/parser/parse.c File Reference
#include <stdlib.h>
#include "class.h"
#include "cbuf.h"
#include "stream.h"
#include "queue.h"
#include "http/parser.h"
#include "http/header.h"
#include "http/interface/http_intro.h"
#include "utils/memory.h"
#include "commons.h"
+ Include dependency graph for http/parser/parse.c:

Go to the source code of this file.

Macros

#define MIN(a, b)   ((a)<(b)? (a) : (b))
 

Functions

ssize_t httpParserParse (void *_this, Stream st)
 

Detailed Description

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file http/parser/parse.c.

Macro Definition Documentation

#define MIN (   a,
 
)    ((a)<(b)? (a) : (b))

Definition at line 37 of file http/parser/parse.c.

Referenced by httpParserParse().

Function Documentation

ssize_t httpParserParse ( void *  _this,
Stream  st 
)

do we have form data??

then parse them...

enqueue current request

prepare for next request

Definition at line 41 of file http/parser/parse.c.

References cbufGetData(), cbufGetLine(), cbufGetRead(), cbufIncRead(), cbufIsEmpty(), cbufIsLocked(), cbufLock(), cbufRead(), cbufRelease(), cbufSetData(), cbufSkipNonAlpha(), CSTRA, FALSE, hashGet(), HTTP_MESSAGE_DONE, HTTP_MESSAGE_GARBAGE, HTTP_MESSAGE_HEADERS_DONE, HTTP_MESSAGE_INTRO_DONE, HTTP_MESSAGE_START, httpParserBody(), httpParserHeader(), httpParserNewMessage(), httpParserPostVars(), httpParserRequestVars(), MEM_FREE, memMalloc(), MIN, queuePut(), and TRUE.

Referenced by httpWorkerProcess().

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 }
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
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: