taskrambler  0.1.9
Web server and task management solution.
utils/http.c File Reference
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <string.h>
#include "http/message.h"
#include "http/request.h"
#include "http/response.h"
#include "class.h"
#include "commons.h"
+ Include dependency graph for utils/http.c:

Go to the source code of this file.

Macros

#define ALPHAVAL(x)    (tolower((x)) - 'a' + 0xa)
 
#define DIGITVAL(x)    ((x) - '0')
 
#define ALNUMVAL(x)    (isdigit((x))?DIGITVAL((x)):ALPHAVAL((x)))
 

Functions

size_t rfc1123Gmt (char *buffer, size_t _nbuf, const time_t *t)
 
size_t rfc1123GmtNow (char *buffer, size_t _nbuf)
 
size_t urldecode (char *buffer, size_t nbuffer)
 
char isHttpVersion (const char *str, size_t len)
 
HttpMessage httpGetMessage (const char *part1, size_t len1, const char *part2, size_t len2, const char *part3, size_t len3)
 

Variables

static const char * DAY_NAMES []
 
static const char * MONTH_NAMES []
 

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 utils/http.c.

Macro Definition Documentation

#define ALNUMVAL (   x)    (isdigit((x))?DIGITVAL((x)):ALPHAVAL((x)))

Definition at line 39 of file utils/http.c.

Referenced by urldecode().

#define ALPHAVAL (   x)    (tolower((x)) - 'a' + 0xa)

Definition at line 37 of file utils/http.c.

#define DIGITVAL (   x)    ((x) - '0')

Definition at line 38 of file utils/http.c.

Function Documentation

HttpMessage httpGetMessage ( const char *  part1,
size_t  len1,
const char *  part2,
size_t  len2,
const char *  part3,
size_t  len3 
)

Definition at line 127 of file utils/http.c.

References isHttpVersion().

Referenced by httpParserNewMessage().

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 }
char isHttpVersion(const char *str, size_t len)
Definition: utils/http.c:112

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char isHttpVersion ( const char *  str,
size_t  len 
)

Definition at line 112 of file utils/http.c.

References FALSE, and TRUE.

Referenced by httpGetMessage(), and httpMessageHasValidVersion().

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 }
#define FALSE
Definition: commons.h:28
#define TRUE
Definition: commons.h:27

+ Here is the caller graph for this function:

size_t rfc1123Gmt ( char *  buffer,
size_t  _nbuf,
const time_t *  t 
)

Definition at line 53 of file utils/http.c.

References DAY_NAMES, and MONTH_NAMES.

Referenced by assetCtor(), httpResponseAsset(), and rfc1123GmtNow().

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 }
static const char * MONTH_NAMES[]
Definition: utils/http.c:44
static const char * DAY_NAMES[]
Definition: utils/http.c:42

+ Here is the caller graph for this function:

size_t rfc1123GmtNow ( char *  buffer,
size_t  _nbuf 
)

Definition at line 69 of file utils/http.c.

References rfc1123Gmt().

Referenced by httpWorkerAddCommonHeader().

70 {
71  time_t t = time(NULL);
72 
73  return rfc1123Gmt(buffer, _nbuf, &t);
74 }
size_t rfc1123Gmt(char *buffer, size_t _nbuf, const time_t *t)
Definition: utils/http.c:53

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t urldecode ( char *  buffer,
size_t  nbuffer 
)

Decode an url encoded string. This expects a valid url encoded string and it size as arguments, else the behaviour of this function is undefined. This function modifies the data in buffer. No copy is made. The reason for this is only performance.

Definition at line 84 of file utils/http.c.

References ALNUMVAL.

Referenced by httpParserPostVars().

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 }
#define ALNUMVAL(x)
Definition: utils/http.c:39

+ Here is the caller graph for this function:

Variable Documentation

const char* DAY_NAMES[]
static
Initial value:
= {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

Definition at line 42 of file utils/http.c.

Referenced by rfc1123Gmt().

const char* MONTH_NAMES[]
static
Initial value:
= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }

Definition at line 44 of file utils/http.c.

Referenced by rfc1123Gmt().