annotate libmpdemux/http.c @ 5408:f9cd6381e327

reworked ADPCM decoders; changes include: * fixed MS IMA ADPCM * dissolved adpcm.c/.h into appropriate ad_* decoders * DK4 audio is handled directly by IMA ADPCM decoder (this obsoletes ad_dk4adpcm.c)
author melanson
date Sat, 30 Mar 2002 22:27:45 +0000
parents f1dea39a50bb
children f716aa9e2df2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
1 /*
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
2 * HTTP Helper
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
4 * (C) 2001, MPlayer team.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
5 */
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
6
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
7 #include <stdio.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
8 #include <stdlib.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
9 #include <string.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
10
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
11 #include "http.h"
4816
f1dea39a50bb Fixed the http response parser when the http header only has the HTTP
bertrand
parents: 4311
diff changeset
12 #include "url.h"
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
13
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
14 HTTP_header_t *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
15 http_new_header() {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
16 HTTP_header_t *http_hdr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
17
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
18 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t));
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
19 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
20 memset( http_hdr, 0, sizeof(HTTP_header_t) );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
21
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
22 return http_hdr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
23 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
24
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
25 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
26 http_free( HTTP_header_t *http_hdr ) {
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
27 HTTP_field_t *field, *field2free;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
28 if( http_hdr==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
29 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
30 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
31 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
32 if( http_hdr->body!=NULL ) free( http_hdr->body );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
33 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
34 if( http_hdr->method!=NULL ) free( http_hdr->method );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
35 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
36 field = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
37 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
38 field2free = field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
39 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
40 free( field2free );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
41 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
42 free( http_hdr );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
43 http_hdr = NULL;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
44 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
45
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
46 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
47 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
48 char *ptr = NULL;
1027
2803b7076c83 Checked the length arg when appending data.
bertrand
parents: 902
diff changeset
49 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
50 ptr = (char*)malloc( http_hdr->buffer_size+length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
51 if( ptr==NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
52 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
53 return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
54 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
55 if( http_hdr->buffer_size==0 ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
56 // Buffer empty, copy response into it.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
57 memcpy( ptr, response, length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
58 http_hdr->buffer_size = length;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
59 } else {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
60 // Buffer not empty, grow buffer, copy and append the response.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
61 memcpy( ptr, http_hdr->buffer, http_hdr->buffer_size );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
62 free( http_hdr->buffer );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
63 memcpy( ptr+http_hdr->buffer_size, response, length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
64 http_hdr->buffer_size += length;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
65 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
66 http_hdr->buffer = ptr;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
67 return http_hdr->buffer_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
68 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
69
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
70 int
2489
0ecc1b4f7cf8 Added ASF http server streaming (Not mms streaming).
bertrand
parents: 2310
diff changeset
71 http_is_header_entire( HTTP_header_t *http_hdr ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
72 if( http_hdr==NULL ) return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
73
3784
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
74 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
75 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
76 return 1;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
77 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
78
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
79 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
80 http_response_parse( HTTP_header_t *http_hdr ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
81 char *hdr_ptr, *ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
82 char *field=NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
83 int pos_hdr_sep, len;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
84 if( http_hdr==NULL ) return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
85 if( http_hdr->is_parsed ) return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
86
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
87 // Get the protocol
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
88 hdr_ptr = strstr( http_hdr->buffer, " " );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
89 if( hdr_ptr==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
90 printf("Malformed answer. No space separator found.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
91 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
92 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
93 len = hdr_ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
94 http_hdr->protocol = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
95 if( http_hdr->protocol==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
96 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
97 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
98 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
99 strncpy( http_hdr->protocol, http_hdr->buffer, len );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
100 http_hdr->protocol[len]='\0';
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
101 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
102 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
103 printf("Malformed answer. Unable to get HTTP minor version.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
104 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
105 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
106 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
107
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
108 // Get the status code
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
109 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
110 printf("Malformed answer. Unable to get status code.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
111 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
112 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
113 hdr_ptr += 4;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
114
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
115 // Get the reason phrase
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
116 ptr = strstr( hdr_ptr, "\n" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
117 if( hdr_ptr==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
118 printf("Malformed answer. Unable to get the reason phrase.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
119 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
120 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
121 len = ptr-hdr_ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
122 http_hdr->reason_phrase = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
123 if( http_hdr->reason_phrase==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
124 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
125 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
126 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
127 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
4311
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
128 if( http_hdr->reason_phrase[len-1]=='\r' ) {
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
129 len--;
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
130 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
131 http_hdr->reason_phrase[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
132
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
133 // Set the position of the header separator: \r\n\r\n
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
134 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
135 if( ptr==NULL ) {
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
136 ptr = strstr( http_hdr->buffer, "\n\n" );
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
137 if( ptr==NULL ) {
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
138 printf("Header may be incomplete. No CRLF CRLF found.\n");
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
139 return -1;
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
140 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
141 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
142 pos_hdr_sep = ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
143
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
144 // Point to the first line after the method line.
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
145 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
146 do {
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
147 ptr = hdr_ptr;
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
148 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
149 len = ptr-hdr_ptr;
4816
f1dea39a50bb Fixed the http response parser when the http header only has the HTTP
bertrand
parents: 4311
diff changeset
150 if( len==0 ) break;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
151 field = (char*)realloc(field, len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
152 if( field==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
153 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
154 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
155 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
156 strncpy( field, hdr_ptr, len );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
157 field[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
158 http_set_field( http_hdr, field );
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
159 hdr_ptr = ptr+((*ptr=='\r')?2:1);
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
160 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
161
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
162 if( field!=NULL ) free( field );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
163
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
164 if( pos_hdr_sep+4<http_hdr->buffer_size ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
165 // Response has data!
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
166 int data_length = http_hdr->buffer_size-(pos_hdr_sep+4);
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
167 http_hdr->body = (char*)malloc( data_length );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
168 if( http_hdr->body==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
169 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
170 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
171 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
172 memcpy( http_hdr->body, http_hdr->buffer+pos_hdr_sep+4, data_length );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
173 http_hdr->body_size = data_length;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
174 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
175
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
176 http_hdr->is_parsed = 1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
177 return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
178 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
179
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
180 char *
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
181 http_build_request( HTTP_header_t *http_hdr ) {
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
182 char *ptr, *uri=NULL;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
183 int len;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
184 HTTP_field_t *field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
185 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
186
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
187 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
188 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
189 else {
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
190 uri = (char*)malloc(strlen(http_hdr->uri)*2);
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
191 if( uri==NULL ) {
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
192 printf("Memory allocation failed\n");
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
193 return NULL;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
194 }
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
195 url_escape_string( uri, http_hdr->uri );
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
196 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
197
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
198 //**** Compute the request length
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
199 // Add the Method line
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
200 len = strlen(http_hdr->method)+strlen(uri)+12;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
201 // Add the fields
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
202 field = http_hdr->first_field;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
203 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
204 len += strlen(field->field_name)+2;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
205 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
206 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
207 // Add the CRLF
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
208 len += 2;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
209 // Add the body
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
210 if( http_hdr->body!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
211 len += http_hdr->body_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
212 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
213 // Free the buffer if it was previously used
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
214 if( http_hdr->buffer!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
215 free( http_hdr->buffer );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
216 http_hdr->buffer = NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
217 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
218 http_hdr->buffer = (char*)malloc(len+1);
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
219 if( http_hdr->buffer==NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
220 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
221 return NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
222 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
223 http_hdr->buffer_size = len;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
224
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
225 //*** Building the request
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
226 ptr = http_hdr->buffer;
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
227 // Add the method line
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
228 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
229 field = http_hdr->first_field;
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
230 // Add the field
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
231 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
232 ptr += sprintf( ptr, "%s\r\n", field->field_name );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
233 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
234 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
235 ptr += sprintf( ptr, "\r\n" );
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
236 // Add the body
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
237 if( http_hdr->body!=NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
238 memcpy( ptr, http_hdr->body, http_hdr->body_size );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
239 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
240
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
241 if( uri ) free( uri );
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
242 return http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
243 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
244
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
245 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
246 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
247 if( http_hdr==NULL || field_name==NULL ) return NULL;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
248 http_hdr->field_search_pos = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
249 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
250 if( http_hdr->field_search==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
251 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
252 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
253 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
254 strcpy( http_hdr->field_search, field_name );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
255 return http_get_next_field( http_hdr );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
256 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
257
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
258 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
259 http_get_next_field( HTTP_header_t *http_hdr ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
260 char *ptr;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
261 HTTP_field_t *field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
262 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
263
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
264 field = http_hdr->field_search_pos;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
265 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
266 ptr = strstr( field->field_name, ":" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
267 if( ptr==NULL ) return NULL;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
268 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
269 ptr++; // Skip the column
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
270 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
271 http_hdr->field_search_pos = field->next;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
272 return ptr; // return the value without the field name
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
273 }
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
274 field = field->next;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
275 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
276 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
277 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
278
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
279 void
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
280 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
281 HTTP_field_t *new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
282 if( http_hdr==NULL || field_name==NULL ) return;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
283
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
284 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t));
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
285 if( new_field==NULL ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
286 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
287 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
288 }
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
289 new_field->next = NULL;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
290 new_field->field_name = (char*)malloc(strlen(field_name)+1);
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
291 if( new_field->field_name==NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
292 printf("Memory allocation failed\n");
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
293 return;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
294 }
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
295 strcpy( new_field->field_name, field_name );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
296
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
297 if( http_hdr->last_field==NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
298 http_hdr->first_field = new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
299 } else {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
300 http_hdr->last_field->next = new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
301 }
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
302 http_hdr->last_field = new_field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
303 http_hdr->field_nb++;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
304 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
305
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
306 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
307 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
308 if( http_hdr==NULL || method==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
309
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
310 http_hdr->method = (char*)malloc(strlen(method)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
311 if( http_hdr->method==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
312 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
313 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
314 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
315 strcpy( http_hdr->method, method );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
316 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
317
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
318 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
319 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
320 if( http_hdr==NULL || uri==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
321
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
322 http_hdr->uri = (char*)malloc(strlen(uri)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
323 if( http_hdr->uri==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
324 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
325 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
326 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
327 strcpy( http_hdr->uri, uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
328 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
329
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
330 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
331 http_debug_hdr( HTTP_header_t *http_hdr ) {
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
332 HTTP_field_t *field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
333 int i = 0;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
334 if( http_hdr==NULL ) return;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
335
4311
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
336 printf("--- HTTP DEBUG HEADER --- START ---\n");
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
337 printf("protocol: [%s]\n", http_hdr->protocol );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
338 printf("http minor version: [%d]\n", http_hdr->http_minor_version );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
339 printf("uri: [%s]\n", http_hdr->uri );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
340 printf("method: [%s]\n", http_hdr->method );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
341 printf("status code: [%d]\n", http_hdr->status_code );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
342 printf("reason phrase: [%s]\n", http_hdr->reason_phrase );
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
343 printf("body size: [%d]\n", http_hdr->body_size );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
344
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
345 printf("Fields:\n");
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
346 field = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
347 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
348 printf(" %d - %s\n", i++, field->field_name );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
349 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
350 }
4311
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
351 printf("--- HTTP DEBUG HEADER --- END ---\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
352 }