taskrambler  0.1.9
Web server and task management solution.
utils/http.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 <time.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <string.h>
28 
29 #include "http/message.h"
30 #include "http/request.h"
31 #include "http/response.h"
32 
33 #include "class.h"
34 
35 #include "commons.h"
36 
37 #define ALPHAVAL(x) (tolower((x)) - 'a' + 0xa)
38 #define DIGITVAL(x) ((x) - '0')
39 #define ALNUMVAL(x) (isdigit((x))?DIGITVAL((x)):ALPHAVAL((x)))
40 
41 
42 static const char *DAY_NAMES[] = {
43  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
44 static const char *MONTH_NAMES[] = {
45  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
46  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
47 
48 
49 /*
50  * This one is not thread save.
51  */
52 size_t
53 rfc1123Gmt(char * buffer, size_t _nbuf, const time_t * t)
54 {
55  struct tm * tmp = gmtime(t);
56  size_t nbuf;
57 
58  nbuf = strftime(buffer, _nbuf, "---, %d --- %Y %T GMT", tmp);
59  memcpy(buffer, DAY_NAMES[tmp->tm_wday], 3);
60  memcpy(buffer+8, MONTH_NAMES[tmp->tm_mon], 3);
61 
62  return nbuf;
63 }
64 
65 /*
66  * This one is not thread save.
67  */
68 size_t
69 rfc1123GmtNow(char * buffer, size_t _nbuf)
70 {
71  time_t t = time(NULL);
72 
73  return rfc1123Gmt(buffer, _nbuf, &t);
74 }
75 
76 /**
77  * Decode an url encoded string. This expects a valid url
78  * encoded string and it size as arguments, else the behaviour
79  * of this function is undefined.
80  * This function modifies the data in buffer. No copy is made.
81  * The reason for this is only performance.
82  */
83 size_t
84 urldecode(char * buffer, size_t nbuffer)
85 {
86  char * buf_ptr = buffer;
87  char * res_ptr = buffer;
88 
89  for(; 0 < nbuffer; nbuffer--, buf_ptr++, res_ptr++) {
90  switch(*buf_ptr) {
91  case '%':
92  *res_ptr = (ALNUMVAL(buf_ptr[1]) << 4) | ALNUMVAL(buf_ptr[2]);
93  buf_ptr += 2;
94  nbuffer -= 2;
95  break;
96 
97  case '+':
98  *buf_ptr = ' ';
99  /* intended drop through */
100 
101  default:
102  *res_ptr = *buf_ptr;
103  break;
104  }
105  }
106  *res_ptr = 0;
107 
108  return res_ptr - buffer;
109 }
110 
111 char
112 isHttpVersion(const char * str, size_t len)
113 {
114  if (NULL == str)
115  return FALSE;
116 
117  if (8 > len)
118  return FALSE;
119 
120  if (0 != memcmp("HTTP/", str, sizeof("HTTP/")-1))
121  return FALSE;
122 
123  return TRUE;
124 }
125 
126 HttpMessage
128  const char * part1, size_t len1,
129  const char * part2, size_t len2,
130  const char * part3, size_t len3)
131 {
132  if (isHttpVersion(part1, len1)) {
133  return new(HttpResponse,
134  part1, len1,
135  strtoul(part2, NULL, 10),
136  part3, len3);
137  }
138 
139  if (isHttpVersion(part3, len3)) {
140  return new(HttpRequest,
141  part1, len1,
142  part2, len2,
143  part3, len3);
144  }
145 
146  return NULL;
147 }
148 
149 // vim: set ts=4 sw=4:
size_t urldecode(char *buffer, size_t nbuffer)
Definition: utils/http.c:84
static const char * MONTH_NAMES[]
Definition: utils/http.c:44
char isHttpVersion(const char *str, size_t len)
Definition: utils/http.c:112
HttpMessage httpGetMessage(const char *part1, size_t len1, const char *part2, size_t len2, const char *part3, size_t len3)
Definition: utils/http.c:127
#define ALNUMVAL(x)
Definition: utils/http.c:39
#define FALSE
Definition: commons.h:28
size_t rfc1123GmtNow(char *buffer, size_t _nbuf)
Definition: utils/http.c:69
static const char * DAY_NAMES[]
Definition: utils/http.c:42
#define TRUE
Definition: commons.h:27
size_t rfc1123Gmt(char *buffer, size_t _nbuf, const time_t *t)
Definition: utils/http.c:53