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