taskrambler  0.1.9
Web server and task management solution.
p_header.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 <string.h>
25 #include <sys/types.h>
26 
27 #include "class.h"
28 #include "http/header.h"
29 #include "http/parser.h"
30 #include "http/message.h"
31 #include "http/request.h"
32 #include "hash.h"
33 
34 #include "utils/memory.h"
35 
36 void
38  HttpParser this,
39  const char * line,
40  const char * lend)
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 }
104 
105 // vim: set ts=4 sw=4:
void * memMalloc(size_t)
Definition: memory.c:783
const char * name
Definition: interface.h:43
void httpParserHeader(HttpParser this, const char *line, const char *lend)
Definition: p_header.c:37
void * hashAdd(Hash, void *)
Definition: add.c:48