1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #define _GNU_SOURCE
5 #include <string.h>
6 #include <errno.h>
7 #include "http_parsing.h"
8 #include "tdate_parse.h"
9
10 #define SPACE_OR_TAB(x) ((x) == ' ' || (x) == '\t')
11 #define CR_OR_NEWLINE(x) ((x) == '\r' || (x) == '\n')
12
13 /*---------------------------------------------------------------------------*/
14 int
find_http_header(char * data,int len)15 find_http_header(char *data, int len)
16 {
17 char *temp = data;
18 int hdr_len = 0;
19 char ch = data[len]; /* remember it */
20
21 /* null terminate the string first */
22 data[len] = 0;
23 while (!hdr_len && (temp = strchr(temp, '\n')) != NULL) {
24 temp++;
25 if (*temp == '\n')
26 hdr_len = temp - data + 1;
27 else if (len > 0 && *temp == '\r' && *(temp + 1) == '\n')
28 hdr_len = temp - data + 2;
29 }
30 data[len] = ch; /* put it back */
31
32 /* terminate the header if found */
33 if (hdr_len)
34 data[hdr_len-1] = 0;
35
36 return hdr_len;
37 }
38 /*--------------------------------------------------------------------------*/
39 char *
http_header_str_val(const char * buf,const char * key,const int keylen,char * value,int value_len)40 http_header_str_val(const char* buf, const char *key, const int keylen,
41 char* value, int value_len)
42 {
43 char *temp = (char *) buf; /* temporarily de-const buf */
44 int i = 0;
45
46 /* find the header field */
47 while (*temp && (temp = strchr(temp, '\n'))) {
48 temp++; /* advance to the next line */
49 if (!strncasecmp(temp, key, keylen))
50 break;
51 }
52 if (temp == NULL || *temp == '\0') {
53 *value = 0;
54 return NULL;
55 }
56
57 /* skip whitespace or tab */
58 temp += keylen;
59 while (*temp && SPACE_OR_TAB(*temp))
60 temp++;
61
62 /* if we reached the end of the line, forget it */
63 if (*temp == '\0' || CR_OR_NEWLINE(*temp)) {
64 *value = 0;
65 return NULL;
66 }
67
68 /* copy value data */
69 while (*temp && !CR_OR_NEWLINE(*temp) && i < value_len-1)
70 value[i++] = *temp++;
71 value[i] = 0;
72 return (i == 0) ? NULL : value;
73 }
74 /*---------------------------------------------------------------------------*/
75 long int
http_header_long_val(const char * response,const char * key,int key_len)76 http_header_long_val(const char * response, const char* key, int key_len)
77 {
78 #define C_TYPE_LEN 50
79 long int len;
80 char value[C_TYPE_LEN];
81 char *temp = http_header_str_val(response, key, key_len, value, C_TYPE_LEN);
82
83 if (temp == NULL)
84 return -1;
85
86 len = strtol(temp, NULL, 10);
87 if (errno == EINVAL || errno == ERANGE)
88 return -1;
89
90 return len;
91 }
92 /*--------------------------------------------------------------------------*/
93 char*
http_get_url(char * data,int data_len,char * value,int value_len)94 http_get_url(char * data, int data_len, char* value, int value_len)
95 {
96 char *ret = data;
97 char *temp;
98 int i = 0;
99
100 if (strncmp(data, HTTP_GET, sizeof(HTTP_GET)-1)) {
101 *value = 0;
102 return NULL;
103 }
104
105 ret += sizeof(HTTP_GET);
106 while (*ret && SPACE_OR_TAB(*ret))
107 ret++;
108
109 temp = ret;
110 while (*temp && *temp != ' ' && i < value_len - 1) {
111 value[i++] = *temp++;
112 }
113 value[i] = 0;
114
115 return ret;
116 }
117 /*---------------------------------------------------------------------------*/
118
119