Mercurial > mplayer.hg
annotate libmpdemux/http.c @ 16062:09ecc9c9df3b
synced with 1.92
author | gabrov |
---|---|
date | Fri, 22 Jul 2005 23:33:20 +0000 |
parents | c5a629a2f33c |
children | d56d00a47568 |
rev | line source |
---|---|
902 | 1 /* |
2 * HTTP Helper | |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
7 #include "config.h" |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
8 |
870 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include <string.h> | |
15585 | 12 #include <unistd.h> |
870 | 13 |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
14 #ifndef HAVE_WINSOCK2 |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
15 #define closesocket close |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
16 #else |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
17 #include <winsock2.h> |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
18 #include <ws2tcpip.h> |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
19 #endif |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
20 |
870 | 21 #include "http.h" |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
22 #include "url.h" |
5915 | 23 #include "mp_msg.h" |
870 | 24 |
15585 | 25 #include "stream.h" |
26 #include "demuxer.h" | |
27 #include "network.h" | |
28 #include "help_mp.h" | |
29 | |
30 | |
31 extern mime_struct_t mime_type_table[]; | |
32 extern int stream_cache_size; | |
33 extern int network_bandwidth; | |
34 | |
35 extern int http_seek(stream_t *stream, off_t pos); | |
36 | |
16013 | 37 typedef struct { |
38 unsigned metaint; | |
39 unsigned metapos; | |
40 int is_ultravox; | |
41 } scast_data_t; | |
42 | |
43 /** | |
44 * \brief first read any data from sc->buffer then from fd | |
45 * \param fd file descriptor to read data from | |
46 * \param buffer buffer to read into | |
47 * \param len how many bytes to read | |
48 * \param sc streaming control containing buffer to read from first | |
49 * \return len unless there is a read error or eof | |
50 */ | |
51 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) { | |
52 unsigned pos = 0; | |
53 unsigned cp_len = sc->buffer_size - sc->buffer_pos; | |
54 if (cp_len > len) | |
55 cp_len = len; | |
56 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len); | |
57 sc->buffer_pos += cp_len; | |
58 pos += cp_len; | |
59 while (pos < len) { | |
60 int ret = read(fd, &buffer[pos], len - pos); | |
61 if (ret <= 0) | |
62 break; | |
63 pos += ret; | |
64 } | |
65 return pos; | |
66 } | |
67 | |
16032 | 68 /** |
69 * \brief read and process (i.e. discard *g*) a block of ultravox metadata | |
70 * \param fd file descriptor to read from | |
71 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
72 * \return number of real data before next metadata block starts or 0 on error | |
73 */ | |
16013 | 74 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { |
75 unsigned metaint; | |
76 unsigned char info[6]; | |
77 do { | |
78 my_read(fd, info, 1, sc); | |
79 if (info[0] == 0x00) | |
80 my_read(fd, info, 6, sc); | |
81 else | |
82 my_read(fd, &info[1], 5, sc); | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
83 // sync byte and reserved flags |
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
84 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { |
16013 | 85 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); |
86 return 0; | |
87 } | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
88 if (info[1] & 0x01) |
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
89 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); |
16013 | 90 metaint = info[4] << 8 | info[5]; |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
91 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense |
16013 | 92 char *metabuf = malloc(metaint); |
93 my_read(fd, metabuf, metaint, sc); | |
94 free(metabuf); | |
95 } | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
96 } while ((info[3] & 0xf) < 0x07); |
16013 | 97 return metaint; |
98 } | |
99 | |
100 /** | |
101 * \brief read one scast meta data entry and print it | |
16032 | 102 * \param fd file descriptor to read from |
103 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
16013 | 104 */ |
105 static void scast_meta_read(int fd, streaming_ctrl_t *sc) { | |
106 unsigned char tmp = 0; | |
107 unsigned metalen; | |
108 my_read(fd, &tmp, 1, sc); | |
109 metalen = tmp * 16; | |
110 if (metalen > 0) { | |
111 char *info = (char *)malloc(metalen + 1); | |
112 unsigned nlen = my_read(fd, info, metalen, sc); | |
113 info[nlen] = 0; | |
114 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); | |
115 free(info); | |
116 } | |
117 } | |
118 | |
16032 | 119 /** |
120 * \brief read data from scast/ultravox stream without any metadata | |
121 * \param fd file descriptor to read from | |
122 * \param buffer buffer to read data into | |
123 * \param size number of bytes to read | |
124 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
125 */ | |
16013 | 126 static int scast_streaming_read(int fd, char *buffer, int size, |
127 streaming_ctrl_t *sc) { | |
128 scast_data_t *sd = (scast_data_t *)sc->data; | |
129 unsigned block, ret; | |
130 unsigned done = 0; | |
131 | |
132 // first read remaining data up to next metadata | |
133 block = sd->metaint - sd->metapos; | |
134 if (block > size) | |
135 block = size; | |
136 ret = my_read(fd, buffer, block, sc); | |
137 sd->metapos += ret; | |
138 done += ret; | |
139 if (ret != block) // read problems or eof | |
140 size = done; | |
141 | |
142 while (done < size) { // now comes the metadata | |
143 if (sd->is_ultravox) | |
144 sd->metaint = uvox_meta_read(fd, sc); | |
145 else | |
146 scast_meta_read(fd, sc); // read and display metadata | |
147 sd->metapos = 0; | |
148 block = size - done; | |
149 if (block > sd->metaint) | |
150 block = sd->metaint; | |
151 ret = my_read(fd, &buffer[done], block, sc); | |
152 sd->metapos += ret; | |
153 done += ret; | |
154 if (ret != block) // read problems or eof | |
155 size = done; | |
156 } | |
157 return done; | |
158 } | |
159 | |
160 static int scast_streaming_start(stream_t *stream) { | |
161 int metaint; | |
162 int fromhdr; | |
163 scast_data_t *scast_data; | |
164 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
165 int is_ultravox = http_hdr && strcasecmp(http_hdr->protocol, "ICY") != 0; | |
166 if (!stream || stream->fd < 0 || !http_hdr) | |
167 return -1; | |
168 if (is_ultravox) | |
169 metaint = 0; | |
170 else { | |
171 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt")); | |
172 if (metaint <= 0) | |
173 return -1; | |
174 } | |
175 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); | |
176 stream->streaming_ctrl->buffer_size = http_hdr->body_size; | |
177 stream->streaming_ctrl->buffer_pos = 0; | |
178 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); | |
179 scast_data = malloc(sizeof(scast_data_t)); | |
180 scast_data->metaint = metaint; | |
181 scast_data->metapos = 0; | |
182 scast_data->is_ultravox = is_ultravox; | |
183 http_free(http_hdr); | |
184 stream->streaming_ctrl->data = scast_data; | |
185 stream->streaming_ctrl->streaming_read = scast_streaming_read; | |
186 stream->streaming_ctrl->streaming_seek = NULL; | |
187 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes | |
188 stream->streaming_ctrl->buffering = 1; | |
189 stream->streaming_ctrl->status = streaming_playing_e; | |
190 return 0; | |
191 } | |
192 | |
15585 | 193 static int nop_streaming_start( stream_t *stream ) { |
194 HTTP_header_t *http_hdr = NULL; | |
195 char *next_url=NULL; | |
196 URL_t *rd_url=NULL; | |
197 int fd,ret; | |
198 if( stream==NULL ) return -1; | |
199 | |
200 fd = stream->fd; | |
201 if( fd<0 ) { | |
202 fd = http_send_request( stream->streaming_ctrl->url, 0 ); | |
203 if( fd<0 ) return -1; | |
204 http_hdr = http_read_response( fd ); | |
205 if( http_hdr==NULL ) return -1; | |
206 | |
207 switch( http_hdr->status_code ) { | |
208 case 200: // OK | |
209 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
210 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
211 if( http_hdr->body_size>0 ) { | |
212 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
213 http_free( http_hdr ); | |
214 return -1; | |
215 } | |
216 } | |
217 break; | |
218 // Redirect | |
219 case 301: // Permanently | |
220 case 302: // Temporarily | |
221 ret=-1; | |
222 next_url = http_get_field( http_hdr, "Location" ); | |
223 | |
224 if (next_url != NULL) | |
225 rd_url=url_new(next_url); | |
226 | |
227 if (next_url != NULL && rd_url != NULL) { | |
228 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); | |
229 stream->streaming_ctrl->url=check4proxies(rd_url); | |
230 ret=nop_streaming_start(stream); //recursively get streaming started | |
231 } else { | |
232 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
233 closesocket( fd ); | |
234 fd = -1; | |
235 } | |
236 return ret; | |
237 break; | |
238 case 401: //Authorization required | |
239 case 403: //Forbidden | |
240 case 404: //Not found | |
241 case 500: //Server Error | |
242 default: | |
243 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
244 closesocket( fd ); | |
245 fd = -1; | |
246 return -1; | |
247 break; | |
248 } | |
249 stream->fd = fd; | |
250 } else { | |
251 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
252 if( http_hdr->body_size>0 ) { | |
253 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
254 http_free( http_hdr ); | |
255 stream->streaming_ctrl->data = NULL; | |
256 return -1; | |
257 } | |
258 } | |
259 } | |
260 | |
261 if( http_hdr ) { | |
262 http_free( http_hdr ); | |
263 stream->streaming_ctrl->data = NULL; | |
264 } | |
265 | |
266 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
267 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
268 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
269 stream->streaming_ctrl->buffering = 1; | |
270 stream->streaming_ctrl->status = streaming_playing_e; | |
271 return 0; | |
272 } | |
273 | |
870 | 274 HTTP_header_t * |
275 http_new_header() { | |
276 HTTP_header_t *http_hdr; | |
277 | |
278 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t)); | |
279 if( http_hdr==NULL ) return NULL; | |
280 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
281 | |
282 return http_hdr; | |
283 } | |
284 | |
285 void | |
286 http_free( HTTP_header_t *http_hdr ) { | |
3039 | 287 HTTP_field_t *field, *field2free; |
870 | 288 if( http_hdr==NULL ) return; |
289 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
290 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
291 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
292 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
902 | 293 if( http_hdr->method!=NULL ) free( http_hdr->method ); |
294 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
3039 | 295 field = http_hdr->first_field; |
296 while( field!=NULL ) { | |
297 field2free = field; | |
14460 | 298 if (field->field_name) |
299 free(field->field_name); | |
3039 | 300 field = field->next; |
301 free( field2free ); | |
302 } | |
870 | 303 free( http_hdr ); |
3039 | 304 http_hdr = NULL; |
870 | 305 } |
306 | |
902 | 307 int |
308 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
1027 | 309 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
310 |
7293 | 311 http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
312 if( http_hdr->buffer==NULL ) { |
7293 | 313 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); |
902 | 314 return -1; |
315 } | |
7293 | 316 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
317 http_hdr->buffer_size += length; | |
318 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
902 | 319 return http_hdr->buffer_size; |
320 } | |
321 | |
322 int | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
323 http_is_header_entire( HTTP_header_t *http_hdr ) { |
902 | 324 if( http_hdr==NULL ) return -1; |
7293 | 325 if( http_hdr->buffer==NULL ) return 0; // empty |
326 | |
3784 | 327 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
328 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
329 return 1; | |
902 | 330 } |
331 | |
332 int | |
333 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 334 char *hdr_ptr, *ptr; |
335 char *field=NULL; | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
336 int pos_hdr_sep, hdr_sep_len, len; |
902 | 337 if( http_hdr==NULL ) return -1; |
338 if( http_hdr->is_parsed ) return 0; | |
870 | 339 |
340 // Get the protocol | |
902 | 341 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 342 if( hdr_ptr==NULL ) { |
5915 | 343 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
902 | 344 return -1; |
870 | 345 } |
902 | 346 len = hdr_ptr-http_hdr->buffer; |
870 | 347 http_hdr->protocol = (char*)malloc(len+1); |
348 if( http_hdr->protocol==NULL ) { | |
5915 | 349 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 350 return -1; |
870 | 351 } |
902 | 352 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
353 http_hdr->protocol[len]='\0'; | |
870 | 354 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
355 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
5915 | 356 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
902 | 357 return -1; |
870 | 358 } |
359 } | |
360 | |
361 // Get the status code | |
362 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
5915 | 363 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
902 | 364 return -1; |
870 | 365 } |
366 hdr_ptr += 4; | |
367 | |
368 // Get the reason phrase | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
369 ptr = strstr( hdr_ptr, "\n" ); |
870 | 370 if( hdr_ptr==NULL ) { |
5915 | 371 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
902 | 372 return -1; |
870 | 373 } |
374 len = ptr-hdr_ptr; | |
375 http_hdr->reason_phrase = (char*)malloc(len+1); | |
376 if( http_hdr->reason_phrase==NULL ) { | |
5915 | 377 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 378 return -1; |
870 | 379 } |
380 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
4311 | 381 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
382 len--; | |
383 } | |
870 | 384 http_hdr->reason_phrase[len]='\0'; |
385 | |
386 // Set the position of the header separator: \r\n\r\n | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
387 hdr_sep_len = 4; |
902 | 388 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 389 if( ptr==NULL ) { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
390 ptr = strstr( http_hdr->buffer, "\n\n" ); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
391 if( ptr==NULL ) { |
5915 | 392 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
|
393 return -1; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
394 } |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
395 hdr_sep_len = 2; |
870 | 396 } |
902 | 397 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 398 |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
399 // 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
|
400 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
870 | 401 do { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
402 ptr = hdr_ptr; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
403 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
870 | 404 len = ptr-hdr_ptr; |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
405 if( len==0 ) break; |
870 | 406 field = (char*)realloc(field, len+1); |
407 if( field==NULL ) { | |
5915 | 408 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 409 return -1; |
870 | 410 } |
411 strncpy( field, hdr_ptr, len ); | |
412 field[len]='\0'; | |
413 http_set_field( http_hdr, field ); | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
414 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
902 | 415 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
870 | 416 |
417 if( field!=NULL ) free( field ); | |
418 | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
419 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { |
870 | 420 // Response has data! |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
421 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; |
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
422 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); |
870 | 423 } |
424 | |
902 | 425 http_hdr->is_parsed = 1; |
426 return 0; | |
870 | 427 } |
428 | |
429 char * | |
902 | 430 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 431 char *ptr, *uri=NULL; |
902 | 432 int len; |
3039 | 433 HTTP_field_t *field; |
870 | 434 if( http_hdr==NULL ) return NULL; |
435 | |
436 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
437 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 438 else { |
12391 | 439 uri = (char*)malloc(strlen(http_hdr->uri) + 1); |
3497 | 440 if( uri==NULL ) { |
5915 | 441 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
3497 | 442 return NULL; |
443 } | |
12391 | 444 strcpy(uri,http_hdr->uri); |
3497 | 445 } |
870 | 446 |
3497 | 447 //**** Compute the request length |
448 // Add the Method line | |
449 len = strlen(http_hdr->method)+strlen(uri)+12; | |
450 // Add the fields | |
451 field = http_hdr->first_field; | |
3039 | 452 while( field!=NULL ) { |
453 len += strlen(field->field_name)+2; | |
454 field = field->next; | |
455 } | |
3497 | 456 // Add the CRLF |
457 len += 2; | |
458 // Add the body | |
902 | 459 if( http_hdr->body!=NULL ) { |
460 len += http_hdr->body_size; | |
461 } | |
3497 | 462 // Free the buffer if it was previously used |
902 | 463 if( http_hdr->buffer!=NULL ) { |
464 free( http_hdr->buffer ); | |
465 http_hdr->buffer = NULL; | |
466 } | |
3497 | 467 http_hdr->buffer = (char*)malloc(len+1); |
902 | 468 if( http_hdr->buffer==NULL ) { |
5915 | 469 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 470 return NULL; |
471 } | |
472 http_hdr->buffer_size = len; | |
473 | |
3497 | 474 //*** Building the request |
902 | 475 ptr = http_hdr->buffer; |
3497 | 476 // Add the method line |
477 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 478 field = http_hdr->first_field; |
3497 | 479 // Add the field |
3039 | 480 while( field!=NULL ) { |
481 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
482 field = field->next; | |
483 } | |
870 | 484 ptr += sprintf( ptr, "\r\n" ); |
3497 | 485 // Add the body |
870 | 486 if( http_hdr->body!=NULL ) { |
487 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
488 } | |
3497 | 489 |
490 if( uri ) free( uri ); | |
902 | 491 return http_hdr->buffer; |
870 | 492 } |
493 | |
494 char * | |
495 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
496 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 497 http_hdr->field_search_pos = http_hdr->first_field; |
498 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
870 | 499 if( http_hdr->field_search==NULL ) { |
5915 | 500 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 501 return NULL; |
502 } | |
503 strcpy( http_hdr->field_search, field_name ); | |
504 return http_get_next_field( http_hdr ); | |
505 } | |
506 | |
507 char * | |
508 http_get_next_field( HTTP_header_t *http_hdr ) { | |
509 char *ptr; | |
3039 | 510 HTTP_field_t *field; |
870 | 511 if( http_hdr==NULL ) return NULL; |
512 | |
3039 | 513 field = http_hdr->field_search_pos; |
514 while( field!=NULL ) { | |
515 ptr = strstr( field->field_name, ":" ); | |
870 | 516 if( ptr==NULL ) return NULL; |
3039 | 517 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 518 ptr++; // Skip the column |
519 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 520 http_hdr->field_search_pos = field->next; |
870 | 521 return ptr; // return the value without the field name |
522 } | |
3039 | 523 field = field->next; |
870 | 524 } |
525 return NULL; | |
526 } | |
527 | |
528 void | |
3039 | 529 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
530 HTTP_field_t *new_field; | |
531 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 532 |
3039 | 533 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t)); |
534 if( new_field==NULL ) { | |
5915 | 535 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 536 return; |
537 } | |
3039 | 538 new_field->next = NULL; |
539 new_field->field_name = (char*)malloc(strlen(field_name)+1); | |
540 if( new_field->field_name==NULL ) { | |
5915 | 541 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
3039 | 542 return; |
543 } | |
544 strcpy( new_field->field_name, field_name ); | |
545 | |
546 if( http_hdr->last_field==NULL ) { | |
547 http_hdr->first_field = new_field; | |
548 } else { | |
549 http_hdr->last_field->next = new_field; | |
550 } | |
551 http_hdr->last_field = new_field; | |
870 | 552 http_hdr->field_nb++; |
553 } | |
554 | |
555 void | |
556 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
557 if( http_hdr==NULL || method==NULL ) return; | |
558 | |
559 http_hdr->method = (char*)malloc(strlen(method)+1); | |
560 if( http_hdr->method==NULL ) { | |
5915 | 561 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 562 return; |
563 } | |
564 strcpy( http_hdr->method, method ); | |
565 } | |
566 | |
567 void | |
568 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
569 if( http_hdr==NULL || uri==NULL ) return; | |
570 | |
571 http_hdr->uri = (char*)malloc(strlen(uri)+1); | |
572 if( http_hdr->uri==NULL ) { | |
5915 | 573 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 574 return; |
575 } | |
576 strcpy( http_hdr->uri, uri ); | |
577 } | |
578 | |
6514 | 579 int |
580 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
581 char *auth, *usr_pass, *b64_usr_pass; | |
582 int encoded_len, pass_len=0, out_len; | |
583 if( http_hdr==NULL || username==NULL ) return -1; | |
584 | |
585 if( password!=NULL ) { | |
586 pass_len = strlen(password); | |
587 } | |
588 | |
589 usr_pass = (char*)malloc(strlen(username)+pass_len+2); | |
590 if( usr_pass==NULL ) { | |
591 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
592 return -1; | |
593 } | |
594 | |
595 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
596 | |
597 // Base 64 encode with at least 33% more data than the original size | |
598 encoded_len = strlen(usr_pass)*2; | |
599 b64_usr_pass = (char*)malloc(encoded_len); | |
600 if( b64_usr_pass==NULL ) { | |
601 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
602 return -1; | |
603 } | |
604 | |
605 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
606 if( out_len<0 ) { | |
607 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
608 return -1; | |
609 } | |
610 | |
611 b64_usr_pass[out_len]='\0'; | |
612 | |
613 auth = (char*)malloc(encoded_len+22); | |
614 if( auth==NULL ) { | |
615 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
616 return -1; | |
617 } | |
618 | |
619 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
620 http_set_field( http_hdr, auth ); | |
621 | |
622 free( usr_pass ); | |
623 free( b64_usr_pass ); | |
624 free( auth ); | |
625 | |
626 return 0; | |
627 } | |
628 | |
870 | 629 void |
630 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 631 HTTP_field_t *field; |
632 int i = 0; | |
902 | 633 if( http_hdr==NULL ) return; |
870 | 634 |
5915 | 635 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
636 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
637 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
638 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
639 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
640 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
641 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
642 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
870 | 643 |
5915 | 644 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
3039 | 645 field = http_hdr->first_field; |
646 while( field!=NULL ) { | |
5915 | 647 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
3039 | 648 field = field->next; |
649 } | |
5915 | 650 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
870 | 651 } |
6514 | 652 |
653 int | |
654 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
655 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
656 | |
657 unsigned char *encBuf; | |
658 int outLen; | |
659 unsigned int bits; | |
660 unsigned int shift; | |
661 | |
662 encBuf = (unsigned char*)enc; | |
663 outLen = 0; | |
664 bits = 0; | |
665 shift = 0; | |
666 | |
667 while( outLen<outMax ) { | |
668 if( encLen>0 ) { | |
669 // Shift in byte | |
670 bits <<= 8; | |
671 bits |= *encBuf; | |
672 shift += 8; | |
673 // Next byte | |
674 encBuf++; | |
675 encLen--; | |
676 } else if( shift>0 ) { | |
677 // Pad last bits to 6 bits - will end next loop | |
678 bits <<= 6 - shift; | |
679 shift = 6; | |
680 } else { | |
681 // Terminate with Mime style '=' | |
682 *out = '='; | |
683 outLen++; | |
684 | |
685 return outLen; | |
686 } | |
687 | |
688 // Encode 6 bit segments | |
689 while( shift>=6 ) { | |
690 shift -= 6; | |
691 *out = b64[ (bits >> shift) & 0x3F ]; | |
692 out++; | |
693 outLen++; | |
694 } | |
695 } | |
696 | |
697 // Output overflow | |
698 return -1; | |
699 } | |
700 | |
15585 | 701 static int http_streaming_start(stream_t *stream, int* file_format) { |
702 HTTP_header_t *http_hdr; | |
703 unsigned int i; | |
704 int fd=-1; | |
705 int redirect = 0; | |
706 int auth_retry=0; | |
707 int seekable=0; | |
708 char *content_type; | |
709 char *next_url; | |
710 URL_t *url = stream->streaming_ctrl->url; | |
6514 | 711 |
15585 | 712 do |
713 { | |
714 fd = http_send_request( url, 0 ); | |
715 if( fd<0 ) { | |
716 return -1; | |
717 } | |
718 | |
719 http_hdr = http_read_response( fd ); | |
720 if( http_hdr==NULL ) { | |
721 closesocket( fd ); | |
722 http_free( http_hdr ); | |
723 return -1; | |
724 } | |
725 | |
726 stream->fd=fd; | |
727 if( verbose>0 ) { | |
728 http_debug_hdr( http_hdr ); | |
729 } | |
730 | |
731 stream->streaming_ctrl->data = (void*)http_hdr; | |
732 | |
733 // Check if we can make partial content requests and thus seek in http-streams | |
734 if( http_hdr!=NULL && http_hdr->status_code==200 ) { | |
735 char *accept_ranges; | |
736 if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) | |
737 seekable = strncmp(accept_ranges,"bytes",5)==0; | |
738 } | |
739 | |
740 // Check if the response is an ICY status_code reason_phrase | |
741 if( !strcasecmp(http_hdr->protocol, "ICY") ) { | |
742 switch( http_hdr->status_code ) { | |
743 case 200: { // OK | |
744 char *field_data = NULL; | |
745 // note: I skip icy-notice1 and 2, as they contain html <BR> | |
746 // and are IMHO useless info ::atmos | |
747 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) | |
748 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); field_data = NULL; | |
749 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) | |
750 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); field_data = NULL; | |
751 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) | |
752 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); field_data = NULL; | |
753 // XXX: does this really mean public server? ::atmos | |
754 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) | |
755 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; | |
756 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) | |
757 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); field_data = NULL; | |
758 | |
759 // If content-type == video/nsv we most likely have a winamp video stream | |
760 // otherwise it should be mp3. if there are more types consider adding mime type | |
761 // handling like later | |
762 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) | |
763 *file_format = DEMUXER_TYPE_NSV; | |
764 else | |
765 *file_format = DEMUXER_TYPE_AUDIO; | |
766 return 0; | |
767 } | |
768 case 400: // Server Full | |
769 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
770 return -1; | |
771 case 401: // Service Unavailable | |
772 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
773 return -1; | |
774 case 403: // Service Forbidden | |
775 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
776 return -1; | |
777 case 404: // Resource Not Found | |
778 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
779 return -1; | |
780 default: | |
781 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
782 return -1; | |
783 } | |
784 } | |
785 | |
786 // Assume standard http if not ICY | |
787 switch( http_hdr->status_code ) { | |
788 case 200: // OK | |
789 // Look if we can use the Content-Type | |
790 content_type = http_get_field( http_hdr, "Content-Type" ); | |
791 if( content_type!=NULL ) { | |
792 char *content_length = NULL; | |
793 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); | |
794 if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) | |
795 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); | |
796 // Check in the mime type table for a demuxer type | |
797 i = 0; | |
798 while(mime_type_table[i].mime_type != NULL) { | |
799 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
800 *file_format = mime_type_table[i].demuxer_type; | |
801 return seekable; | |
802 } | |
803 i++; | |
804 } | |
805 } | |
806 // Not found in the mime type table, don't fail, | |
807 // we should try raw HTTP | |
808 return seekable; | |
809 // Redirect | |
810 case 301: // Permanently | |
811 case 302: // Temporarily | |
812 // TODO: RFC 2616, recommand to detect infinite redirection loops | |
813 next_url = http_get_field( http_hdr, "Location" ); | |
814 if( next_url!=NULL ) { | |
815 closesocket( fd ); | |
816 url_free( url ); | |
817 stream->streaming_ctrl->url = url = url_new( next_url ); | |
818 http_free( http_hdr ); | |
819 redirect = 1; | |
820 } | |
821 break; | |
822 case 401: // Authentication required | |
823 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return STREAM_UNSUPORTED; | |
824 redirect = 1; | |
825 break; | |
826 default: | |
827 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
828 return -1; | |
829 } | |
830 } while( redirect ); | |
831 | |
832 return -1; | |
833 } | |
834 | |
835 static int fixup_open(stream_t *stream,int seekable) { | |
16013 | 836 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; |
837 int is_icy = http_hdr && strcasecmp(http_hdr->protocol, "ICY") == 0; | |
838 char *content_type = http_get_field( http_hdr, "Content-Type" ); | |
839 int is_ultravox = http_hdr && content_type && | |
840 strcasecmp(content_type, "misc/ultravox") == 0; | |
15585 | 841 |
842 stream->type = STREAMTYPE_STREAM; | |
16013 | 843 if(!is_icy && !is_ultravox && seekable) |
15585 | 844 { |
845 stream->flags |= STREAM_SEEK; | |
846 stream->seek = http_seek; | |
847 } | |
848 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
16013 | 849 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) |
15585 | 850 if(nop_streaming_start( stream )) { |
851 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
852 streaming_ctrl_free(stream->streaming_ctrl); | |
853 stream->streaming_ctrl = NULL; | |
854 return STREAM_UNSUPORTED; | |
855 } | |
856 | |
857 fixup_network_stream_cache(stream); | |
858 return STREAM_OK; | |
859 } | |
860 | |
861 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
862 int seekable=0; | |
863 URL_t *url; | |
864 | |
865 stream->streaming_ctrl = streaming_ctrl_new(); | |
866 if( stream->streaming_ctrl==NULL ) { | |
867 return STREAM_ERROR; | |
868 } | |
869 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
870 url = url_new(stream->url); | |
871 stream->streaming_ctrl->url = check4proxies(url); | |
872 //url_free(url); | |
873 | |
874 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(1), URL: %s\n", stream->url); | |
875 seekable = http_streaming_start(stream, file_format); | |
876 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
877 streaming_ctrl_free(stream->streaming_ctrl); | |
878 stream->streaming_ctrl = NULL; | |
879 return STREAM_UNSUPORTED; | |
880 } | |
881 | |
882 return fixup_open(stream, seekable); | |
883 } | |
884 | |
885 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
886 int seekable=0; | |
887 URL_t *url; | |
888 | |
889 stream->streaming_ctrl = streaming_ctrl_new(); | |
890 if( stream->streaming_ctrl==NULL ) { | |
891 return STREAM_ERROR; | |
892 } | |
893 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
894 url = url_new(stream->url); | |
895 stream->streaming_ctrl->url = check4proxies(url); | |
896 //url_free(url); | |
897 | |
898 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_HTTP(2), URL: %s\n", stream->url); | |
899 seekable = http_streaming_start(stream, file_format); | |
900 if(seekable < 0) { | |
901 streaming_ctrl_free(stream->streaming_ctrl); | |
902 stream->streaming_ctrl = NULL; | |
903 return STREAM_UNSUPORTED; | |
904 } | |
905 | |
906 return fixup_open(stream, seekable); | |
907 } | |
908 | |
909 | |
910 stream_info_t stream_info_http1 = { | |
911 "http streaming", | |
912 "null", | |
913 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
914 "plain http", | |
915 open_s1, | |
16013 | 916 {"http", "http_proxy", "unsv", NULL}, |
15585 | 917 NULL, |
918 0 // Urls are an option string | |
919 }; | |
920 | |
921 stream_info_t stream_info_http2 = { | |
922 "http streaming", | |
923 "null", | |
924 "Bertrand, Albeu, Arpi? who?", | |
925 "plain http, aslo used as falback for many other protocols", | |
926 open_s2, | |
927 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
928 NULL, | |
929 0 // Urls are an option string | |
930 }; |