taskrambler  0.1.9
Web server and task management solution.
parser.h File Reference
#include "class.h"
#include "http/message.h"
#include "queue.h"
#include "cbuf.h"
#include "stream.h"
#include "commons.h"
+ Include dependency graph for parser.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PARSER_MAX_BUF   131072
 

Enumerations

enum  HttpMessageState {
  HTTP_MESSAGE_GARBAGE =0, HTTP_MESSAGE_START, HTTP_MESSAGE_INTRO_DONE, HTTP_MESSAGE_HEADERS_DONE,
  HTTP_MESSAGE_DONE
}
 

Functions

 CLASS (HttpParser)
 
ssize_t httpParserParse (void *, Stream)
 
void httpParserRequestVars (HttpParser)
 
void httpParserHeader (HttpParser, const char *, const char *)
 
void httpParserNewMessage (HttpParser, const char *, const char *lend)
 
size_t httpParserBody (HttpParser, const char *, size_t)
 
void httpParserPostVars (HttpParser)
 

Detailed Description

Parse requests from an input stream.

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 parser.h.

Macro Definition Documentation

#define PARSER_MAX_BUF   131072

Definition at line 35 of file parser.h.

Referenced by httpWorkerCtor().

Enumeration Type Documentation

Enumerator
HTTP_MESSAGE_GARBAGE 
HTTP_MESSAGE_START 
HTTP_MESSAGE_INTRO_DONE 
HTTP_MESSAGE_HEADERS_DONE 
HTTP_MESSAGE_DONE 

Definition at line 38 of file parser.h.

Function Documentation

CLASS ( HttpParser  )

Definition at line 47 of file parser.h.

References Bool.

47  {
48  Cbuf buffer;
49  Bool ourLock;
50 
51  char * incomplete;
52  size_t isize;
53 
54  Queue queue;
55  HttpMessage current;
56 
57  HttpMessageState state;
58 };
HttpMessageState
Definition: parser.h:38
#define Bool
Definition: commons.h:26
size_t httpParserBody ( HttpParser  ,
const char *  ,
size_t   
)

Definition at line 34 of file p_body.c.

References MIN.

Referenced by httpParserParse().

35 {
36  size_t len = 0;
37  HttpMessage current = this->current;
38 
39  if (current->dbody < current->nbody) {
40  len = MIN(current->nbody - current->dbody, nbuf);
41 
42  memcpy(current->body, buf, len);
43 
44  current->dbody += len;
45  }
46 
47  return len;
48 }
#define MIN(a, b)
Definition: commons.h:35

+ Here is the caller graph for this function:

void httpParserHeader ( HttpParser  ,
const char *  ,
const char *   
)

Definition at line 37 of file p_header.c.

References hashAdd(), memMalloc(), and interface::name.

Referenced by httpParserParse().

41 {
42  const char * name = line;
43  char * value = memchr(line, ':', lend - line);
44  size_t nname = (value++) - name;
45  HttpMessage current = this->current;
46 
47  if (NULL == value) {
48  return;
49  }
50 
51  for (; *value == ' ' && value < lend; value++);
52 
53  if (value == lend) {
54  return;
55  }
56 
57  if (0 == strncasecmp("content-length", name, nname-1)) {
58  current->nbody = strtoul(value, NULL, 10);
59  if (0 < this->current->nbody) {
60  current->body = memMalloc(current->nbody);
61  }
62  current->dbody = 0;
63  }
64 
65  if (0 == strncasecmp("cookie", name, nname-1)) {
66  HttpRequest request = (HttpRequest)this->current;
67  char * pair = value;
68  ssize_t togo = lend - value;
69 
70  while(NULL != pair && 0 < togo) {
71  char * key = pair;
72  char * eqsign;
73  char * val;
74  size_t nval;
75 
76  for (; *key == ' ' && key < lend; key++, togo--);
77  eqsign = memchr(key, '=', togo);
78 
79  if (NULL == eqsign) {
80  break;
81  }
82 
83  togo -= (eqsign - key);
84  pair = memchr(eqsign, ';', togo);
85 
86  if (NULL == pair) {
87  pair = (char *)lend;
88  }
89 
90  nval = pair-eqsign-1;
91  val = (0 != nval)? eqsign+1 : NULL;
92 
93  hashAdd(request->cookies,
94  new(HashValue, key, eqsign-key, val, nval));
95 
96  pair++;
97  togo -= (pair - eqsign);
98  }
99  }
100 
101  hashAdd(current->header,
102  new(HttpHeader, name, nname, value, lend - value));
103 }
void * memMalloc(size_t)
Definition: memory.c:783
void * hashAdd(Hash, void *)
Definition: add.c:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void httpParserNewMessage ( HttpParser  ,
const char *  ,
const char *  lend 
)

Definition at line 28 of file new_message.c.

References httpGetMessage().

Referenced by httpParserParse().

32 {
33  const char * part1, * part2, * part3;
34  size_t len1, len2, len3;
35 
36  part1 = line;
37  part2 = memchr(line, ' ', lend - line);
38 
39  if (NULL == part2) return;
40 
41  len1 = part2 - part1;
42  for (; *part2 == ' ' && *part2 != 0; part2++);
43 
44  part3 = memchr(part2, ' ', lend - part2);
45 
46  if (NULL == part3) return;
47 
48  len2 = part3 - part2;
49  for (; *part3 == ' ' && *part3 != 0; part3++);
50 
51  len3 = lend - part3;
52 
53  this->current = httpGetMessage(part1, len1, part2, len2, part3, len3);
54 }
HttpMessage httpGetMessage(const char *, size_t, const char *, size_t, const char *, size_t)
Definition: utils/http.c:127

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t httpParserParse ( void *  ,
Stream   
)

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:

void httpParserPostVars ( HttpParser  this)
Todo:
this is very similar to other pair parsing things... key1=val1<delim>key2=val2<delim>...keyn=valn Generalize this!!!!

Definition at line 39 of file p_post_vars.c.

References hashAdd(), and urldecode().

Referenced by httpParserParse().

40 {
41  HttpRequest request = (HttpRequest)this->current;
42  char * pair = this->current->body;
43  ssize_t togo = this->current->nbody;
44 
45  while(NULL != pair && 0 < togo) {
46  char * key = pair;
47  char * eqsign = memchr(key, '=', togo);
48  char * value;
49  size_t nvalue;
50 
51  if (NULL == eqsign) {
52  return;
53  }
54 
55  togo -= (eqsign - key);
56  pair = memchr(eqsign, '&', togo);
57 
58  if (NULL == pair) {
59  pair = &(this->current->body[this->current->nbody]);
60  }
61 
62  nvalue = pair-eqsign-1;
63  value = (0 != nvalue)? eqsign+1 : NULL;
64  nvalue = urldecode(value, nvalue);
65 
66  hashAdd(request->post,
67  new(HashValue, key, eqsign-key, value, nvalue));
68 
69  pair++;
70  togo -= (pair - eqsign);
71  }
72 }
size_t urldecode(char *, size_t)
Definition: utils/http.c:84
void * hashAdd(Hash, void *)
Definition: add.c:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void httpParserRequestVars ( HttpParser  )

Definition at line 35 of file p_request_vars.c.

References hashAdd(), and memMalloc().

Referenced by httpParserParse().

36 {
37  HttpRequest request = (HttpRequest)this->current;
38  char * delim = strchr(request->uri, '?');
39 
40  if (NULL == delim) {
41  delim = request->uri + strlen(request->uri);
42  }
43 
44  request->path = memMalloc(delim - request->uri + 1);
45  request->path[delim - request->uri] = 0;
46  memcpy(request->path, request->uri, delim - request->uri);
47 
48  while(NULL != delim && 0 != *delim) {
49  char * key = delim + 1;
50  char * eqsign = strchr(key, '=');
51  char * value;
52  size_t nvalue;
53 
54  if (NULL == eqsign) {
55  return;
56  }
57 
58  delim = strchr(eqsign, '&');
59 
60  if (NULL == delim) {
61  delim = key + strlen(key);
62  }
63 
64  nvalue = delim-eqsign-1;
65  value = (0 != nvalue)? eqsign+1 : NULL;
66 
67  hashAdd(request->get,
68  new(HashValue, key, eqsign-key, value, nvalue));
69  }
70 }
void * memMalloc(size_t)
Definition: memory.c:783
void * hashAdd(Hash, void *)
Definition: add.c:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function: