4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
41 #include "libmpdemux/demuxer.h"
44 #include "libavutil/base64.h"
46 #include <libavutil/avutil.h>
48 extern int stream_cache_size
;
49 extern int network_bandwidth
;
58 * \brief first read any data from sc->buffer then from fd
59 * \param fd file descriptor to read data from
60 * \param buffer buffer to read into
61 * \param len how many bytes to read
62 * \param sc streaming control containing buffer to read from first
63 * \return len unless there is a read error or eof
65 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
67 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
70 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
71 sc
->buffer_pos
+= cp_len
;
74 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
83 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
84 * \param fd file descriptor to read from
85 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
86 * \return number of real data before next metadata block starts or 0 on error
88 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
90 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
93 info_read
= my_read(fd
, info
, 1, sc
);
95 info_read
= my_read(fd
, info
, 6, sc
);
97 info_read
+= my_read(fd
, &info
[1], 5, sc
);
98 if (info_read
!= 6) // read error or eof
100 // sync byte and reserved flags
101 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
102 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
106 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
107 metaint
= info
[4] << 8 | info
[5];
108 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
109 char *metabuf
= malloc(metaint
);
110 my_read(fd
, metabuf
, metaint
, sc
);
113 } while ((info
[3] & 0xf) < 0x07);
118 * \brief read one scast meta data entry and print it
119 * \param fd file descriptor to read from
120 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
122 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
123 unsigned char tmp
= 0;
125 my_read(fd
, &tmp
, 1, sc
);
129 uint8_t *info
= malloc(metalen
+ 1);
130 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
131 // avoid breaking the user's terminal too much
132 if (nlen
> 256) nlen
= 256;
133 for (i
= 0; i
< nlen
; i
++)
134 if (info
[i
] && info
[i
] < 32) info
[i
] = '?';
136 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
142 * \brief read data from scast/ultravox stream without any metadata
143 * \param fd file descriptor to read from
144 * \param buffer buffer to read data into
145 * \param size number of bytes to read
146 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
148 static int scast_streaming_read(int fd
, char *buffer
, int size
,
149 streaming_ctrl_t
*sc
) {
150 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
154 // first read remaining data up to next metadata
155 block
= sd
->metaint
- sd
->metapos
;
158 ret
= my_read(fd
, buffer
, block
, sc
);
161 if (ret
!= block
) // read problems or eof
164 while (done
< size
) { // now comes the metadata
167 sd
->metaint
= uvox_meta_read(fd
, sc
);
172 scast_meta_read(fd
, sc
); // read and display metadata
175 if (block
> sd
->metaint
)
177 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
180 if (ret
!= block
) // read problems or eof
186 static int scast_streaming_start(stream_t
*stream
) {
188 scast_data_t
*scast_data
;
189 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
190 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
191 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
196 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
200 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
201 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
202 stream
->streaming_ctrl
->buffer_pos
= 0;
203 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
204 scast_data
= malloc(sizeof(scast_data_t
));
205 scast_data
->metaint
= metaint
;
206 scast_data
->metapos
= 0;
207 scast_data
->is_ultravox
= is_ultravox
;
209 stream
->streaming_ctrl
->data
= scast_data
;
210 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
211 stream
->streaming_ctrl
->streaming_seek
= NULL
;
212 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
213 stream
->streaming_ctrl
->buffering
= 1;
214 stream
->streaming_ctrl
->status
= streaming_playing_e
;
218 static int nop_streaming_start( stream_t
*stream
) {
219 HTTP_header_t
*http_hdr
= NULL
;
223 if( stream
==NULL
) return -1;
227 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
228 if( fd
<0 ) return -1;
229 http_hdr
= http_read_response( fd
);
230 if( http_hdr
==NULL
) return -1;
232 switch( http_hdr
->status_code
) {
234 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
235 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
236 if( http_hdr
->body_size
>0 ) {
237 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
238 http_free( http_hdr
);
244 case 301: // Permanently
245 case 302: // Temporarily
246 case 303: // See Other
247 case 307: // Temporarily (since HTTP/1.1)
249 next_url
= http_get_field( http_hdr
, "Location" );
251 if (next_url
!= NULL
)
252 rd_url
=url_new(next_url
);
254 if (next_url
!= NULL
&& rd_url
!= NULL
) {
255 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
256 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
257 ret
=nop_streaming_start(stream
); //recursively get streaming started
259 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
265 case 401: //Authorization required
266 case 403: //Forbidden
267 case 404: //Not found
268 case 500: //Server Error
270 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
278 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
279 if( http_hdr
->body_size
>0 ) {
280 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
281 http_free( http_hdr
);
282 stream
->streaming_ctrl
->data
= NULL
;
289 http_free( http_hdr
);
290 stream
->streaming_ctrl
->data
= NULL
;
293 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
294 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
295 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
296 stream
->streaming_ctrl
->buffering
= 1;
297 stream
->streaming_ctrl
->status
= streaming_playing_e
;
302 http_new_header(void) {
303 HTTP_header_t
*http_hdr
;
305 http_hdr
= calloc(1, sizeof(*http_hdr
));
306 if( http_hdr
==NULL
) return NULL
;
312 http_free( HTTP_header_t
*http_hdr
) {
313 HTTP_field_t
*field
, *field2free
;
314 if( http_hdr
==NULL
) return;
315 free(http_hdr
->protocol
);
317 free(http_hdr
->reason_phrase
);
318 free(http_hdr
->field_search
);
319 free(http_hdr
->method
);
320 free(http_hdr
->buffer
);
321 field
= http_hdr
->first_field
;
322 while( field
!=NULL
) {
324 free(field
->field_name
);
333 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
334 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
336 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
337 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
340 http_hdr
->buffer
= realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
341 if( http_hdr
->buffer
==NULL
) {
342 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
345 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
346 http_hdr
->buffer_size
+= length
;
347 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
348 return http_hdr
->buffer_size
;
352 http_is_header_entire( HTTP_header_t
*http_hdr
) {
353 if( http_hdr
==NULL
) return -1;
354 if( http_hdr
->buffer
==NULL
) return 0; // empty
356 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
357 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
362 http_response_parse( HTTP_header_t
*http_hdr
) {
365 int pos_hdr_sep
, hdr_sep_len
;
367 if( http_hdr
==NULL
) return -1;
368 if( http_hdr
->is_parsed
) return 0;
371 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
372 if( hdr_ptr
==NULL
) {
373 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
376 len
= hdr_ptr
-http_hdr
->buffer
;
377 http_hdr
->protocol
= malloc(len
+1);
378 if( http_hdr
->protocol
==NULL
) {
379 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
382 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
383 http_hdr
->protocol
[len
]='\0';
384 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
385 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
386 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
391 // Get the status code
392 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
393 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
398 // Get the reason phrase
399 ptr
= strstr( hdr_ptr
, "\n" );
400 if( hdr_ptr
==NULL
) {
401 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
405 http_hdr
->reason_phrase
= malloc(len
+1);
406 if( http_hdr
->reason_phrase
==NULL
) {
407 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
410 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
411 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
414 http_hdr
->reason_phrase
[len
]='\0';
416 // Set the position of the header separator: \r\n\r\n
418 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
420 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
422 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
427 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
429 // Point to the first line after the method line.
430 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
433 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
436 field
= realloc(field
, len
+1);
438 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
441 strncpy( field
, hdr_ptr
, len
);
443 http_set_field( http_hdr
, field
);
444 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
445 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
449 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
450 // Response has data!
451 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
452 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
455 http_hdr
->is_parsed
= 1;
460 http_build_request( HTTP_header_t
*http_hdr
) {
461 char *ptr
, *uri
=NULL
;
464 if( http_hdr
==NULL
) return NULL
;
466 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
467 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
469 uri
= malloc(strlen(http_hdr
->uri
) + 1);
471 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
474 strcpy(uri
,http_hdr
->uri
);
477 //**** Compute the request length
478 // Add the Method line
479 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
481 field
= http_hdr
->first_field
;
482 while( field
!=NULL
) {
483 len
+= strlen(field
->field_name
)+2;
489 if( http_hdr
->body
!=NULL
) {
490 len
+= http_hdr
->body_size
;
492 // Free the buffer if it was previously used
493 if( http_hdr
->buffer
!=NULL
) {
494 free( http_hdr
->buffer
);
495 http_hdr
->buffer
= NULL
;
497 http_hdr
->buffer
= malloc(len
+1);
498 if( http_hdr
->buffer
==NULL
) {
499 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
502 http_hdr
->buffer_size
= len
;
504 //*** Building the request
505 ptr
= http_hdr
->buffer
;
506 // Add the method line
507 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
508 field
= http_hdr
->first_field
;
510 while( field
!=NULL
) {
511 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
514 ptr
+= sprintf( ptr
, "\r\n" );
516 if( http_hdr
->body
!=NULL
) {
517 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
521 return http_hdr
->buffer
;
525 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
526 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
527 http_hdr
->field_search_pos
= http_hdr
->first_field
;
528 http_hdr
->field_search
= realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
529 if( http_hdr
->field_search
==NULL
) {
530 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
533 strcpy( http_hdr
->field_search
, field_name
);
534 return http_get_next_field( http_hdr
);
538 http_get_next_field( HTTP_header_t
*http_hdr
) {
541 if( http_hdr
==NULL
) return NULL
;
543 field
= http_hdr
->field_search_pos
;
544 while( field
!=NULL
) {
545 ptr
= strstr( field
->field_name
, ":" );
546 if( ptr
==NULL
) return NULL
;
547 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
548 ptr
++; // Skip the column
549 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
550 http_hdr
->field_search_pos
= field
->next
;
551 return ptr
; // return the value without the field name
559 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
560 HTTP_field_t
*new_field
;
561 if( http_hdr
==NULL
|| field_name
==NULL
) return;
563 new_field
= malloc(sizeof(HTTP_field_t
));
564 if( new_field
==NULL
) {
565 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
568 new_field
->next
= NULL
;
569 new_field
->field_name
= malloc(strlen(field_name
)+1);
570 if( new_field
->field_name
==NULL
) {
571 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
575 strcpy( new_field
->field_name
, field_name
);
577 if( http_hdr
->last_field
==NULL
) {
578 http_hdr
->first_field
= new_field
;
580 http_hdr
->last_field
->next
= new_field
;
582 http_hdr
->last_field
= new_field
;
583 http_hdr
->field_nb
++;
587 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
588 if( http_hdr
==NULL
|| method
==NULL
) return;
590 http_hdr
->method
= malloc(strlen(method
)+1);
591 if( http_hdr
->method
==NULL
) {
592 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
595 strcpy( http_hdr
->method
, method
);
599 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
600 if( http_hdr
==NULL
|| uri
==NULL
) return;
602 http_hdr
->uri
= malloc(strlen(uri
)+1);
603 if( http_hdr
->uri
==NULL
) {
604 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
607 strcpy( http_hdr
->uri
, uri
);
611 http_add_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
, const char *auth_str
) {
612 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
613 int encoded_len
, pass_len
=0;
614 size_t auth_len
, usr_pass_len
;
616 if( http_hdr
==NULL
|| username
==NULL
) return -1;
618 if( password
!=NULL
) {
619 pass_len
= strlen(password
);
622 usr_pass_len
= strlen(username
) + 1 + pass_len
;
623 usr_pass
= malloc(usr_pass_len
+ 1);
624 if( usr_pass
==NULL
) {
625 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
629 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
631 encoded_len
= AV_BASE64_SIZE(usr_pass_len
);
632 b64_usr_pass
= malloc(encoded_len
);
633 if( b64_usr_pass
==NULL
) {
634 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
637 av_base64_encode(b64_usr_pass
, encoded_len
, usr_pass
, usr_pass_len
);
639 auth_len
= encoded_len
+ 100;
640 auth
= malloc(auth_len
);
642 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
646 snprintf(auth
, auth_len
, "%s: Basic %s", auth_str
, b64_usr_pass
);
647 http_set_field( http_hdr
, auth
);
652 free( b64_usr_pass
);
659 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
660 return http_add_authentication(http_hdr
, username
, password
, "Authorization");
664 http_add_basic_proxy_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
665 return http_add_authentication(http_hdr
, username
, password
, "Proxy-Authorization");
669 http_debug_hdr( HTTP_header_t
*http_hdr
) {
672 if( http_hdr
==NULL
) return;
674 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
675 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
676 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
677 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
678 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
679 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
680 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
681 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%zd]\n", http_hdr
->body_size
);
683 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
684 field
= http_hdr
->first_field
;
685 while( field
!=NULL
) {
686 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
689 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
692 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
693 const char *field_data
;
694 // note: I skip icy-notice1 and 2, as they contain html <BR>
695 // and are IMHO useless info ::atmos
696 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
697 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
698 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
699 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
700 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
701 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
702 // XXX: does this really mean public server? ::atmos
703 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
704 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
705 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
706 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
709 //! If this function succeeds you must closesocket stream->fd
710 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
711 HTTP_header_t
*http_hdr
= NULL
;
713 int res
= STREAM_UNSUPPORTED
;
718 const char *content_length
;
720 URL_t
*url
= stream
->streaming_ctrl
->url
;
725 if (fd
> 0) closesocket(fd
);
726 fd
= http_send_request( url
, 0 );
732 http_hdr
= http_read_response( fd
);
733 if( http_hdr
==NULL
) {
737 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
738 http_debug_hdr( http_hdr
);
741 // Check if we can make partial content requests and thus seek in http-streams
742 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
743 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
744 const char *server
= http_get_field(http_hdr
, "Server");
746 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
747 else if (server
&& (strcmp(server
, "gvs 1.0") == 0 ||
748 strncmp(server
, "MakeMKV", 7) == 0)) {
749 // HACK for youtube and MakeMKV incorrectly claiming not to support seeking
750 mp_msg(MSGT_NETWORK
, MSGL_WARN
, "Broken webserver, incorrectly claims to not support Accept-Ranges\n");
755 print_icy_metadata(http_hdr
);
757 // Check if the response is an ICY status_code reason_phrase
758 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
759 http_get_field(http_hdr
, "Icy-MetaInt") ) {
760 switch( http_hdr
->status_code
) {
763 // If content-type == video/nsv we most likely have a winamp video stream
764 // otherwise it should be mp3. if there are more types consider adding mime type
765 // handling like later
766 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
767 *file_format
= DEMUXER_TYPE_NSV
;
768 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
769 *file_format
= DEMUXER_TYPE_AAC
;
771 *file_format
= DEMUXER_TYPE_AUDIO
;
775 case 400: // Server Full
776 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
778 case 401: // Service Unavailable
779 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
781 case 403: // Service Forbidden
782 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
784 case 404: // Resource Not Found
785 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
788 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
793 // Assume standard http if not ICY
794 switch( http_hdr
->status_code
) {
796 content_length
= http_get_field(http_hdr
, "Content-Length");
797 if (content_length
) {
798 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", content_length
);
799 stream
->end_pos
= atoll(content_length
);
801 // Look if we can use the Content-Type
802 content_type
= http_get_field( http_hdr
, "Content-Type" );
803 if( content_type
!=NULL
) {
806 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
807 // Check in the mime type table for a demuxer type
808 for (i
= 0; mime_type_table
[i
].mime_type
!= NULL
; i
++) {
809 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
810 *file_format
= mime_type_table
[i
].demuxer_type
;
816 // Not found in the mime type table, don't fail,
817 // we should try raw HTTP
821 case 301: // Permanently
822 case 302: // Temporarily
823 case 303: // See Other
824 case 307: // Temporarily (since HTTP/1.1)
825 // TODO: RFC 2616, recommand to detect infinite redirection loops
826 next_url
= http_get_field( http_hdr
, "Location" );
827 if( next_url
!=NULL
) {
828 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
829 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
830 if (!strcasecmp(url
->protocol
, "mms")) {
831 res
= STREAM_REDIRECTED
;
834 if (strcasecmp(url
->protocol
, "http")) {
835 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
840 url
->protocol
= strdup("unsv");
845 case 401: // Authentication required
846 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
851 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
857 if (fd
> 0) closesocket( fd
);
859 http_free( http_hdr
);
862 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
867 static int fixup_open(stream_t
*stream
,int seekable
) {
868 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
869 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
870 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
872 stream
->type
= STREAMTYPE_STREAM
;
873 if(!is_icy
&& !is_ultravox
&& seekable
)
875 stream
->flags
|= MP_STREAM_SEEK
;
876 stream
->seek
= http_seek
;
878 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
879 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
880 if(nop_streaming_start( stream
)) {
881 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
883 closesocket(stream
->fd
);
885 streaming_ctrl_free(stream
->streaming_ctrl
);
886 stream
->streaming_ctrl
= NULL
;
887 return STREAM_UNSUPPORTED
;
890 fixup_network_stream_cache(stream
);
894 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
898 stream
->streaming_ctrl
= streaming_ctrl_new();
899 if( stream
->streaming_ctrl
==NULL
) {
902 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
903 url
= url_new(stream
->url
);
904 stream
->streaming_ctrl
->url
= check4proxies(url
);
907 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
908 seekable
= http_streaming_start(stream
, file_format
);
909 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
911 closesocket(stream
->fd
);
913 if (seekable
== STREAM_REDIRECTED
)
915 streaming_ctrl_free(stream
->streaming_ctrl
);
916 stream
->streaming_ctrl
= NULL
;
917 return STREAM_UNSUPPORTED
;
920 return fixup_open(stream
, seekable
);
923 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
927 stream
->streaming_ctrl
= streaming_ctrl_new();
928 if( stream
->streaming_ctrl
==NULL
) {
931 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
932 url
= url_new(stream
->url
);
933 stream
->streaming_ctrl
->url
= check4proxies(url
);
936 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
937 seekable
= http_streaming_start(stream
, file_format
);
940 closesocket(stream
->fd
);
942 streaming_ctrl_free(stream
->streaming_ctrl
);
943 stream
->streaming_ctrl
= NULL
;
944 return STREAM_UNSUPPORTED
;
947 return fixup_open(stream
, seekable
);
951 const stream_info_t stream_info_http1
= {
954 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
957 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
959 0 // Urls are an option string
962 const stream_info_t stream_info_http2
= {
965 "Bertrand, Albeu, Arpi? who?",
966 "plain http, also used as fallback for many other protocols",
968 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
970 0 // Urls are an option string