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