Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 3343:58ab1490a0be
dunnowhat Q/A de biztos jo
author | gabucino |
---|---|
date | Wed, 05 Dec 2001 23:07:05 +0000 |
parents | 80189681c02b |
children | 6c1e57bdbd96 |
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 | |
73 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL ) return 0; | |
74 else return 1; | |
75 } | |
76 | |
77 int | |
78 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 79 char *hdr_ptr, *ptr; |
80 char *field=NULL; | |
81 int pos_hdr_sep, len; | |
902 | 82 if( http_hdr==NULL ) return -1; |
83 if( http_hdr->is_parsed ) return 0; | |
870 | 84 |
85 // Get the protocol | |
902 | 86 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 87 if( hdr_ptr==NULL ) { |
902 | 88 printf("Malformed answer. No space separator found.\n"); |
89 return -1; | |
870 | 90 } |
902 | 91 len = hdr_ptr-http_hdr->buffer; |
870 | 92 http_hdr->protocol = (char*)malloc(len+1); |
93 if( http_hdr->protocol==NULL ) { | |
94 printf("Memory allocation failed\n"); | |
902 | 95 return -1; |
870 | 96 } |
902 | 97 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
98 http_hdr->protocol[len]='\0'; | |
870 | 99 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
100 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
902 | 101 printf("Malformed answer. Unable to get HTTP minor version.\n"); |
102 return -1; | |
870 | 103 } |
104 } | |
105 | |
106 // Get the status code | |
107 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
902 | 108 printf("Malformed answer. Unable to get status code.\n"); |
109 return -1; | |
870 | 110 } |
111 hdr_ptr += 4; | |
112 | |
113 // Get the reason phrase | |
114 ptr = strstr( hdr_ptr, "\r\n" ); | |
115 if( hdr_ptr==NULL ) { | |
902 | 116 printf("Malformed answer. Unable to get the reason phrase.\n"); |
117 return -1; | |
870 | 118 } |
119 len = ptr-hdr_ptr; | |
120 http_hdr->reason_phrase = (char*)malloc(len+1); | |
121 if( http_hdr->reason_phrase==NULL ) { | |
122 printf("Memory allocation failed\n"); | |
902 | 123 return -1; |
870 | 124 } |
125 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
126 http_hdr->reason_phrase[len]='\0'; | |
127 | |
128 // Set the position of the header separator: \r\n\r\n | |
902 | 129 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 130 if( ptr==NULL ) { |
902 | 131 printf("Header may be incomplete. No CRLF CRLF found.\n"); |
132 return -1; | |
870 | 133 } |
902 | 134 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 135 |
902 | 136 hdr_ptr = strstr( http_hdr->buffer, "\r\n" )+2; |
870 | 137 do { |
138 ptr = strstr( hdr_ptr, "\r\n"); | |
139 if( ptr==NULL ) { | |
140 printf("No CRLF found\n"); | |
902 | 141 return -1; |
870 | 142 } |
143 len = ptr-hdr_ptr; | |
144 field = (char*)realloc(field, len+1); | |
145 if( field==NULL ) { | |
146 printf("Memory allocation failed\n"); | |
902 | 147 return -1; |
870 | 148 } |
149 strncpy( field, hdr_ptr, len ); | |
150 field[len]='\0'; | |
151 http_set_field( http_hdr, field ); | |
152 hdr_ptr = ptr+2; | |
902 | 153 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
870 | 154 |
155 if( field!=NULL ) free( field ); | |
156 | |
902 | 157 if( pos_hdr_sep+4<http_hdr->buffer_size ) { |
870 | 158 // Response has data! |
902 | 159 int data_length = http_hdr->buffer_size-(pos_hdr_sep+4); |
870 | 160 http_hdr->body = (char*)malloc( data_length ); |
161 if( http_hdr->body==NULL ) { | |
902 | 162 printf("Memory allocation failed\n"); |
163 return -1; | |
870 | 164 } |
902 | 165 memcpy( http_hdr->body, http_hdr->buffer+pos_hdr_sep+4, data_length ); |
870 | 166 http_hdr->body_size = data_length; |
167 } | |
168 | |
902 | 169 http_hdr->is_parsed = 1; |
170 return 0; | |
870 | 171 } |
172 | |
173 char * | |
902 | 174 http_build_request( HTTP_header_t *http_hdr ) { |
870 | 175 char *ptr; |
902 | 176 int len; |
3039 | 177 HTTP_field_t *field; |
870 | 178 if( http_hdr==NULL ) return NULL; |
179 | |
180 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
181 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
182 | |
902 | 183 // Compute the request length |
184 len = strlen(http_hdr->method)+strlen(http_hdr->uri)+12; // Method line | |
3039 | 185 field = http_hdr->first_field; // Fields |
186 while( field!=NULL ) { | |
187 len += strlen(field->field_name)+2; | |
188 field = field->next; | |
189 } | |
902 | 190 len += 2; // CRLF |
191 if( http_hdr->body!=NULL ) { | |
192 len += http_hdr->body_size; | |
193 } | |
194 if( http_hdr->buffer!=NULL ) { | |
195 free( http_hdr->buffer ); | |
196 http_hdr->buffer = NULL; | |
197 } | |
198 http_hdr->buffer = (char*)malloc(len); | |
199 if( http_hdr->buffer==NULL ) { | |
200 printf("Memory allocation failed\n"); | |
201 return NULL; | |
202 } | |
203 http_hdr->buffer_size = len; | |
204 | |
205 ptr = http_hdr->buffer; | |
870 | 206 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, http_hdr->uri, http_hdr->http_minor_version ); |
3039 | 207 field = http_hdr->first_field; |
208 while( field!=NULL ) { | |
209 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
210 field = field->next; | |
211 } | |
870 | 212 ptr += sprintf( ptr, "\r\n" ); |
213 if( http_hdr->body!=NULL ) { | |
214 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
215 } | |
902 | 216 return http_hdr->buffer; |
870 | 217 } |
218 | |
219 char * | |
220 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
221 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 222 http_hdr->field_search_pos = http_hdr->first_field; |
223 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 224 if( http_hdr->field_search==NULL ) { |
225 printf("Memory allocation failed\n"); | |
226 return NULL; | |
227 } | |
228 strcpy( http_hdr->field_search, field_name ); | |
229 return http_get_next_field( http_hdr ); | |
230 } | |
231 | |
232 char * | |
233 http_get_next_field( HTTP_header_t *http_hdr ) { | |
234 char *ptr; | |
235 int i; | |
3039 | 236 HTTP_field_t *field; |
870 | 237 if( http_hdr==NULL ) return NULL; |
238 | |
3039 | 239 field = http_hdr->field_search_pos; |
240 while( field!=NULL ) { | |
241 ptr = strstr( field->field_name, ":" ); | |
870 | 242 if( ptr==NULL ) return NULL; |
3039 | 243 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 244 ptr++; // Skip the column |
245 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 246 http_hdr->field_search_pos = field->next; |
870 | 247 return ptr; // return the value without the field name |
248 } | |
3039 | 249 field = field->next; |
870 | 250 } |
251 return NULL; | |
252 } | |
253 | |
254 void | |
3039 | 255 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
256 HTTP_field_t *new_field; | |
257 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 258 |
3039 | 259 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
260 if( new_field==NULL ) { | |
870 | 261 printf("Memory allocation failed\n"); |
262 return; | |
263 } | |
3039 | 264 new_field->next = NULL; |
265 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
266 if( new_field->field_name==NULL ) { | |
267 printf("Memory allocation failed\n"); | |
268 return; | |
269 } | |
270 strcpy( new_field->field_name, field_name ); | |
271 | |
272 if( http_hdr->last_field==NULL ) { | |
273 http_hdr->first_field = new_field; | |
274 } else { | |
275 http_hdr->last_field->next = new_field; | |
276 } | |
277 http_hdr->last_field = new_field; | |
870 | 278 http_hdr->field_nb++; |
279 } | |
280 | |
281 void | |
282 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
283 if( http_hdr==NULL || method==NULL ) return; | |
284 | |
285 http_hdr->method = (char*)malloc(strlen(method)+1); | |
286 if( http_hdr->method==NULL ) { | |
287 printf("Memory allocation failed\n"); | |
288 return; | |
289 } | |
290 strcpy( http_hdr->method, method ); | |
291 } | |
292 | |
293 void | |
294 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
295 if( http_hdr==NULL || uri==NULL ) return; | |
296 | |
297 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
298 if( http_hdr->uri==NULL ) { | |
299 printf("Memory allocation failed\n"); | |
300 return; | |
301 } | |
302 strcpy( http_hdr->uri, uri ); | |
303 } | |
304 | |
305 void | |
306 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 307 HTTP_field_t *field; |
308 int i = 0; | |
902 | 309 if( http_hdr==NULL ) return; |
870 | 310 |
311 printf("protocol: %s\n", http_hdr->protocol ); | |
312 printf("http minor version: %d\n", http_hdr->http_minor_version ); | |
313 printf("uri: %s\n", http_hdr->uri ); | |
314 printf("method: %s\n", http_hdr->method ); | |
315 printf("status code: %d\n", http_hdr->status_code ); | |
316 printf("reason phrase: %s\n", http_hdr->reason_phrase ); | |
317 | |
318 printf("Fields:\n"); | |
3039 | 319 field = http_hdr->first_field; |
320 while( field!=NULL ) { | |
321 printf(" %d - %s\n", i++, field->field_name ); | |
322 field = field->next; | |
323 } | |
870 | 324 } |