3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
25 #include "libmpdemux/demuxer.h"
30 extern const mime_struct_t mime_type_table
[];
31 extern int stream_cache_size
;
32 extern int network_bandwidth
;
34 int http_seek(stream_t
*stream
, off_t pos
);
43 * \brief first read any data from sc->buffer then from fd
44 * \param fd file descriptor to read data from
45 * \param buffer buffer to read into
46 * \param len how many bytes to read
47 * \param sc streaming control containing buffer to read from first
48 * \return len unless there is a read error or eof
50 static unsigned my_read(int fd
, char *buffer
, int len
, streaming_ctrl_t
*sc
) {
52 unsigned cp_len
= sc
->buffer_size
- sc
->buffer_pos
;
55 memcpy(buffer
, &sc
->buffer
[sc
->buffer_pos
], cp_len
);
56 sc
->buffer_pos
+= cp_len
;
59 int ret
= recv(fd
, &buffer
[pos
], len
- pos
, 0);
68 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
69 * \param fd file descriptor to read from
70 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
71 * \return number of real data before next metadata block starts or 0 on error
73 static unsigned uvox_meta_read(int fd
, streaming_ctrl_t
*sc
) {
75 unsigned char info
[6] = {0, 0, 0, 0, 0, 0};
78 info_read
= my_read(fd
, info
, 1, sc
);
80 info_read
= my_read(fd
, info
, 6, sc
);
82 info_read
+= my_read(fd
, &info
[1], 5, sc
);
83 if (info_read
!= 6) // read error or eof
85 // sync byte and reserved flags
86 if (info
[0] != 0x5a || (info
[1] & 0xfc) != 0x00) {
87 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid or unknown uvox metadata\n");
91 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Encrypted ultravox data\n");
92 metaint
= info
[4] << 8 | info
[5];
93 if ((info
[3] & 0xf) < 0x07) { // discard any metadata nonsense
94 char *metabuf
= malloc(metaint
);
95 my_read(fd
, metabuf
, metaint
, sc
);
98 } while ((info
[3] & 0xf) < 0x07);
103 * \brief read one scast meta data entry and print it
104 * \param fd file descriptor to read from
105 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
107 static void scast_meta_read(int fd
, streaming_ctrl_t
*sc
) {
108 unsigned char tmp
= 0;
110 my_read(fd
, &tmp
, 1, sc
);
113 char *info
= malloc(metalen
+ 1);
114 unsigned nlen
= my_read(fd
, info
, metalen
, sc
);
116 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "\nICY Info: %s\n", info
);
122 * \brief read data from scast/ultravox stream without any metadata
123 * \param fd file descriptor to read from
124 * \param buffer buffer to read data into
125 * \param size number of bytes to read
126 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
128 static int scast_streaming_read(int fd
, char *buffer
, int size
,
129 streaming_ctrl_t
*sc
) {
130 scast_data_t
*sd
= (scast_data_t
*)sc
->data
;
134 // first read remaining data up to next metadata
135 block
= sd
->metaint
- sd
->metapos
;
138 ret
= my_read(fd
, buffer
, block
, sc
);
141 if (ret
!= block
) // read problems or eof
144 while (done
< size
) { // now comes the metadata
147 sd
->metaint
= uvox_meta_read(fd
, sc
);
152 scast_meta_read(fd
, sc
); // read and display metadata
155 if (block
> sd
->metaint
)
157 ret
= my_read(fd
, &buffer
[done
], block
, sc
);
160 if (ret
!= block
) // read problems or eof
166 static int scast_streaming_start(stream_t
*stream
) {
168 scast_data_t
*scast_data
;
169 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
170 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
171 if (!stream
|| stream
->fd
< 0 || !http_hdr
)
176 metaint
= atoi(http_get_field(http_hdr
, "Icy-MetaInt"));
180 stream
->streaming_ctrl
->buffer
= malloc(http_hdr
->body_size
);
181 stream
->streaming_ctrl
->buffer_size
= http_hdr
->body_size
;
182 stream
->streaming_ctrl
->buffer_pos
= 0;
183 memcpy(stream
->streaming_ctrl
->buffer
, http_hdr
->body
, http_hdr
->body_size
);
184 scast_data
= malloc(sizeof(scast_data_t
));
185 scast_data
->metaint
= metaint
;
186 scast_data
->metapos
= 0;
187 scast_data
->is_ultravox
= is_ultravox
;
189 stream
->streaming_ctrl
->data
= scast_data
;
190 stream
->streaming_ctrl
->streaming_read
= scast_streaming_read
;
191 stream
->streaming_ctrl
->streaming_seek
= NULL
;
192 stream
->streaming_ctrl
->prebuffer_size
= 64 * 1024; // 64 KBytes
193 stream
->streaming_ctrl
->buffering
= 1;
194 stream
->streaming_ctrl
->status
= streaming_playing_e
;
198 static int nop_streaming_start( stream_t
*stream
) {
199 HTTP_header_t
*http_hdr
= NULL
;
203 if( stream
==NULL
) return -1;
207 fd
= http_send_request( stream
->streaming_ctrl
->url
, 0 );
208 if( fd
<0 ) return -1;
209 http_hdr
= http_read_response( fd
);
210 if( http_hdr
==NULL
) return -1;
212 switch( http_hdr
->status_code
) {
214 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
215 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
216 if( http_hdr
->body_size
>0 ) {
217 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
218 http_free( http_hdr
);
224 case 301: // Permanently
225 case 302: // Temporarily
226 case 303: // See Other
228 next_url
= http_get_field( http_hdr
, "Location" );
230 if (next_url
!= NULL
)
231 rd_url
=url_new(next_url
);
233 if (next_url
!= NULL
&& rd_url
!= NULL
) {
234 mp_msg(MSGT_NETWORK
,MSGL_STATUS
,"Redirected: Using this url instead %s\n",next_url
);
235 stream
->streaming_ctrl
->url
=check4proxies(rd_url
);
236 ret
=nop_streaming_start(stream
); //recursively get streaming started
238 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Redirection failed\n");
244 case 401: //Authorization required
245 case 403: //Forbidden
246 case 404: //Not found
247 case 500: //Server Error
249 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned code %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
257 http_hdr
= (HTTP_header_t
*)stream
->streaming_ctrl
->data
;
258 if( http_hdr
->body_size
>0 ) {
259 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
260 http_free( http_hdr
);
261 stream
->streaming_ctrl
->data
= NULL
;
268 http_free( http_hdr
);
269 stream
->streaming_ctrl
->data
= NULL
;
272 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
273 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
274 stream
->streaming_ctrl
->prebuffer_size
= 64*1024; // 64 KBytes
275 stream
->streaming_ctrl
->buffering
= 1;
276 stream
->streaming_ctrl
->status
= streaming_playing_e
;
281 http_new_header(void) {
282 HTTP_header_t
*http_hdr
;
284 http_hdr
= malloc(sizeof(HTTP_header_t
));
285 if( http_hdr
==NULL
) return NULL
;
286 memset( http_hdr
, 0, sizeof(HTTP_header_t
) );
292 http_free( HTTP_header_t
*http_hdr
) {
293 HTTP_field_t
*field
, *field2free
;
294 if( http_hdr
==NULL
) return;
295 if( http_hdr
->protocol
!=NULL
) free( http_hdr
->protocol
);
296 if( http_hdr
->uri
!=NULL
) free( http_hdr
->uri
);
297 if( http_hdr
->reason_phrase
!=NULL
) free( http_hdr
->reason_phrase
);
298 if( http_hdr
->field_search
!=NULL
) free( http_hdr
->field_search
);
299 if( http_hdr
->method
!=NULL
) free( http_hdr
->method
);
300 if( http_hdr
->buffer
!=NULL
) free( http_hdr
->buffer
);
301 field
= http_hdr
->first_field
;
302 while( field
!=NULL
) {
304 if (field
->field_name
)
305 free(field
->field_name
);
314 http_response_append( HTTP_header_t
*http_hdr
, char *response
, int length
) {
315 if( http_hdr
==NULL
|| response
==NULL
|| length
<0 ) return -1;
317 if( (unsigned)length
> SIZE_MAX
- http_hdr
->buffer_size
- 1) {
318 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Bad size in memory (re)allocation\n");
321 http_hdr
->buffer
= (char*)realloc( http_hdr
->buffer
, http_hdr
->buffer_size
+length
+1 );
322 if( http_hdr
->buffer
==NULL
) {
323 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory (re)allocation failed\n");
326 memcpy( http_hdr
->buffer
+http_hdr
->buffer_size
, response
, length
);
327 http_hdr
->buffer_size
+= length
;
328 http_hdr
->buffer
[http_hdr
->buffer_size
]=0; // close the string!
329 return http_hdr
->buffer_size
;
333 http_is_header_entire( HTTP_header_t
*http_hdr
) {
334 if( http_hdr
==NULL
) return -1;
335 if( http_hdr
->buffer
==NULL
) return 0; // empty
337 if( strstr(http_hdr
->buffer
, "\r\n\r\n")==NULL
&&
338 strstr(http_hdr
->buffer
, "\n\n")==NULL
) return 0;
343 http_response_parse( HTTP_header_t
*http_hdr
) {
346 int pos_hdr_sep
, hdr_sep_len
;
348 if( http_hdr
==NULL
) return -1;
349 if( http_hdr
->is_parsed
) return 0;
352 hdr_ptr
= strstr( http_hdr
->buffer
, " " );
353 if( hdr_ptr
==NULL
) {
354 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. No space separator found.\n");
357 len
= hdr_ptr
-http_hdr
->buffer
;
358 http_hdr
->protocol
= malloc(len
+1);
359 if( http_hdr
->protocol
==NULL
) {
360 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
363 strncpy( http_hdr
->protocol
, http_hdr
->buffer
, len
);
364 http_hdr
->protocol
[len
]='\0';
365 if( !strncasecmp( http_hdr
->protocol
, "HTTP", 4) ) {
366 if( sscanf( http_hdr
->protocol
+5,"1.%d", &(http_hdr
->http_minor_version
) )!=1 ) {
367 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get HTTP minor version.\n");
372 // Get the status code
373 if( sscanf( ++hdr_ptr
, "%d", &(http_hdr
->status_code
) )!=1 ) {
374 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get status code.\n");
379 // Get the reason phrase
380 ptr
= strstr( hdr_ptr
, "\n" );
381 if( hdr_ptr
==NULL
) {
382 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Malformed answer. Unable to get the reason phrase.\n");
386 http_hdr
->reason_phrase
= malloc(len
+1);
387 if( http_hdr
->reason_phrase
==NULL
) {
388 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
391 strncpy( http_hdr
->reason_phrase
, hdr_ptr
, len
);
392 if( http_hdr
->reason_phrase
[len
-1]=='\r' ) {
395 http_hdr
->reason_phrase
[len
]='\0';
397 // Set the position of the header separator: \r\n\r\n
399 ptr
= strstr( http_hdr
->buffer
, "\r\n\r\n" );
401 ptr
= strstr( http_hdr
->buffer
, "\n\n" );
403 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Header may be incomplete. No CRLF CRLF found.\n");
408 pos_hdr_sep
= ptr
-http_hdr
->buffer
;
410 // Point to the first line after the method line.
411 hdr_ptr
= strstr( http_hdr
->buffer
, "\n" )+1;
414 while( *ptr
!='\r' && *ptr
!='\n' ) ptr
++;
417 field
= (char*)realloc(field
, len
+1);
419 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
422 strncpy( field
, hdr_ptr
, len
);
424 http_set_field( http_hdr
, field
);
425 hdr_ptr
= ptr
+((*ptr
=='\r')?2:1);
426 } while( hdr_ptr
<(http_hdr
->buffer
+pos_hdr_sep
) );
428 if( field
!=NULL
) free( field
);
430 if( pos_hdr_sep
+hdr_sep_len
<http_hdr
->buffer_size
) {
431 // Response has data!
432 http_hdr
->body
= http_hdr
->buffer
+pos_hdr_sep
+hdr_sep_len
;
433 http_hdr
->body_size
= http_hdr
->buffer_size
-(pos_hdr_sep
+hdr_sep_len
);
436 http_hdr
->is_parsed
= 1;
441 http_build_request( HTTP_header_t
*http_hdr
) {
442 char *ptr
, *uri
=NULL
;
445 if( http_hdr
==NULL
) return NULL
;
447 if( http_hdr
->method
==NULL
) http_set_method( http_hdr
, "GET");
448 if( http_hdr
->uri
==NULL
) http_set_uri( http_hdr
, "/");
450 uri
= malloc(strlen(http_hdr
->uri
) + 1);
452 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
455 strcpy(uri
,http_hdr
->uri
);
458 //**** Compute the request length
459 // Add the Method line
460 len
= strlen(http_hdr
->method
)+strlen(uri
)+12;
462 field
= http_hdr
->first_field
;
463 while( field
!=NULL
) {
464 len
+= strlen(field
->field_name
)+2;
470 if( http_hdr
->body
!=NULL
) {
471 len
+= http_hdr
->body_size
;
473 // Free the buffer if it was previously used
474 if( http_hdr
->buffer
!=NULL
) {
475 free( http_hdr
->buffer
);
476 http_hdr
->buffer
= NULL
;
478 http_hdr
->buffer
= malloc(len
+1);
479 if( http_hdr
->buffer
==NULL
) {
480 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Memory allocation failed\n");
483 http_hdr
->buffer_size
= len
;
485 //*** Building the request
486 ptr
= http_hdr
->buffer
;
487 // Add the method line
488 ptr
+= sprintf( ptr
, "%s %s HTTP/1.%d\r\n", http_hdr
->method
, uri
, http_hdr
->http_minor_version
);
489 field
= http_hdr
->first_field
;
491 while( field
!=NULL
) {
492 ptr
+= sprintf( ptr
, "%s\r\n", field
->field_name
);
495 ptr
+= sprintf( ptr
, "\r\n" );
497 if( http_hdr
->body
!=NULL
) {
498 memcpy( ptr
, http_hdr
->body
, http_hdr
->body_size
);
501 if( uri
) free( uri
);
502 return http_hdr
->buffer
;
506 http_get_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
507 if( http_hdr
==NULL
|| field_name
==NULL
) return NULL
;
508 http_hdr
->field_search_pos
= http_hdr
->first_field
;
509 http_hdr
->field_search
= (char*)realloc( http_hdr
->field_search
, strlen(field_name
)+1 );
510 if( http_hdr
->field_search
==NULL
) {
511 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
514 strcpy( http_hdr
->field_search
, field_name
);
515 return http_get_next_field( http_hdr
);
519 http_get_next_field( HTTP_header_t
*http_hdr
) {
522 if( http_hdr
==NULL
) return NULL
;
524 field
= http_hdr
->field_search_pos
;
525 while( field
!=NULL
) {
526 ptr
= strstr( field
->field_name
, ":" );
527 if( ptr
==NULL
) return NULL
;
528 if( !strncasecmp( field
->field_name
, http_hdr
->field_search
, ptr
-(field
->field_name
) ) ) {
529 ptr
++; // Skip the column
530 while( ptr
[0]==' ' ) ptr
++; // Skip the spaces if there is some
531 http_hdr
->field_search_pos
= field
->next
;
532 return ptr
; // return the value without the field name
540 http_set_field( HTTP_header_t
*http_hdr
, const char *field_name
) {
541 HTTP_field_t
*new_field
;
542 if( http_hdr
==NULL
|| field_name
==NULL
) return;
544 new_field
= malloc(sizeof(HTTP_field_t
));
545 if( new_field
==NULL
) {
546 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
549 new_field
->next
= NULL
;
550 new_field
->field_name
= malloc(strlen(field_name
)+1);
551 if( new_field
->field_name
==NULL
) {
552 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
556 strcpy( new_field
->field_name
, field_name
);
558 if( http_hdr
->last_field
==NULL
) {
559 http_hdr
->first_field
= new_field
;
561 http_hdr
->last_field
->next
= new_field
;
563 http_hdr
->last_field
= new_field
;
564 http_hdr
->field_nb
++;
568 http_set_method( HTTP_header_t
*http_hdr
, const char *method
) {
569 if( http_hdr
==NULL
|| method
==NULL
) return;
571 http_hdr
->method
= malloc(strlen(method
)+1);
572 if( http_hdr
->method
==NULL
) {
573 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
576 strcpy( http_hdr
->method
, method
);
580 http_set_uri( HTTP_header_t
*http_hdr
, const char *uri
) {
581 if( http_hdr
==NULL
|| uri
==NULL
) return;
583 http_hdr
->uri
= malloc(strlen(uri
)+1);
584 if( http_hdr
->uri
==NULL
) {
585 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
588 strcpy( http_hdr
->uri
, uri
);
592 http_add_basic_authentication( HTTP_header_t
*http_hdr
, const char *username
, const char *password
) {
593 char *auth
= NULL
, *usr_pass
= NULL
, *b64_usr_pass
= NULL
;
594 int encoded_len
, pass_len
=0, out_len
;
596 if( http_hdr
==NULL
|| username
==NULL
) return -1;
598 if( password
!=NULL
) {
599 pass_len
= strlen(password
);
602 usr_pass
= malloc(strlen(username
)+pass_len
+2);
603 if( usr_pass
==NULL
) {
604 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
608 sprintf( usr_pass
, "%s:%s", username
, (password
==NULL
)?"":password
);
610 // Base 64 encode with at least 33% more data than the original size
611 encoded_len
= strlen(usr_pass
)*2;
612 b64_usr_pass
= malloc(encoded_len
);
613 if( b64_usr_pass
==NULL
) {
614 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
618 out_len
= base64_encode( usr_pass
, strlen(usr_pass
), b64_usr_pass
, encoded_len
);
620 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Base64 out overflow\n");
624 b64_usr_pass
[out_len
]='\0';
626 auth
= malloc(encoded_len
+22);
628 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed\n");
632 sprintf( auth
, "Authorization: Basic %s", b64_usr_pass
);
633 http_set_field( http_hdr
, auth
);
638 free( b64_usr_pass
);
645 http_debug_hdr( HTTP_header_t
*http_hdr
) {
648 if( http_hdr
==NULL
) return;
650 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- START ---\n");
651 mp_msg(MSGT_NETWORK
,MSGL_V
,"protocol: [%s]\n", http_hdr
->protocol
);
652 mp_msg(MSGT_NETWORK
,MSGL_V
,"http minor version: [%d]\n", http_hdr
->http_minor_version
);
653 mp_msg(MSGT_NETWORK
,MSGL_V
,"uri: [%s]\n", http_hdr
->uri
);
654 mp_msg(MSGT_NETWORK
,MSGL_V
,"method: [%s]\n", http_hdr
->method
);
655 mp_msg(MSGT_NETWORK
,MSGL_V
,"status code: [%d]\n", http_hdr
->status_code
);
656 mp_msg(MSGT_NETWORK
,MSGL_V
,"reason phrase: [%s]\n", http_hdr
->reason_phrase
);
657 mp_msg(MSGT_NETWORK
,MSGL_V
,"body size: [%d]\n", http_hdr
->body_size
);
659 mp_msg(MSGT_NETWORK
,MSGL_V
,"Fields:\n");
660 field
= http_hdr
->first_field
;
661 while( field
!=NULL
) {
662 mp_msg(MSGT_NETWORK
,MSGL_V
," %d - %s\n", i
++, field
->field_name
);
665 mp_msg(MSGT_NETWORK
,MSGL_V
,"--- HTTP DEBUG HEADER --- END ---\n");
669 base64_encode(const void *enc
, int encLen
, char *out
, int outMax
) {
670 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
672 unsigned char *encBuf
;
677 encBuf
= (unsigned char*)enc
;
692 } else if( shift
>0 ) {
693 // Pad last bits to 6 bits - will end next loop
697 // As per RFC 2045, section 6.8,
698 // pad output as necessary: 0 to 2 '=' chars.
707 // Encode 6 bit segments
709 if (outLen
>= outMax
)
712 *out
= b64
[ (bits
>> shift
) & 0x3F ];
719 static void print_icy_metadata(HTTP_header_t
*http_hdr
) {
720 const char *field_data
;
721 // note: I skip icy-notice1 and 2, as they contain html <BR>
722 // and are IMHO useless info ::atmos
723 if( (field_data
= http_get_field(http_hdr
, "icy-name")) != NULL
)
724 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Name : %s\n", field_data
);
725 if( (field_data
= http_get_field(http_hdr
, "icy-genre")) != NULL
)
726 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Genre : %s\n", field_data
);
727 if( (field_data
= http_get_field(http_hdr
, "icy-url")) != NULL
)
728 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Website: %s\n", field_data
);
729 // XXX: does this really mean public server? ::atmos
730 if( (field_data
= http_get_field(http_hdr
, "icy-pub")) != NULL
)
731 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Public : %s\n", atoi(field_data
)?"yes":"no");
732 if( (field_data
= http_get_field(http_hdr
, "icy-br")) != NULL
)
733 mp_msg(MSGT_NETWORK
,MSGL_INFO
,"Bitrate: %skbit/s\n", field_data
);
736 //! If this function succeeds you must closesocket stream->fd
737 static int http_streaming_start(stream_t
*stream
, int* file_format
) {
738 HTTP_header_t
*http_hdr
= NULL
;
741 int res
= STREAM_UNSUPPORTED
;
747 URL_t
*url
= stream
->streaming_ctrl
->url
;
752 if (fd
> 0) closesocket(fd
);
753 fd
= http_send_request( url
, 0 );
759 http_hdr
= http_read_response( fd
);
760 if( http_hdr
==NULL
) {
764 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
765 http_debug_hdr( http_hdr
);
768 // Check if we can make partial content requests and thus seek in http-streams
769 if( http_hdr
!=NULL
&& http_hdr
->status_code
==200 ) {
770 const char *accept_ranges
= http_get_field(http_hdr
,"Accept-Ranges");
771 const char *server
= http_get_field(http_hdr
, "Server");
773 seekable
= strncmp(accept_ranges
,"bytes",5)==0;
774 else if (server
&& strcmp(server
, "gvs 1.0") == 0)
775 seekable
= 1; // HACK for youtube incorrectly claiming not to support seeking
778 print_icy_metadata(http_hdr
);
780 // Check if the response is an ICY status_code reason_phrase
781 if( !strcasecmp(http_hdr
->protocol
, "ICY") ||
782 http_get_field(http_hdr
, "Icy-MetaInt") ) {
783 switch( http_hdr
->status_code
) {
786 // If content-type == video/nsv we most likely have a winamp video stream
787 // otherwise it should be mp3. if there are more types consider adding mime type
788 // handling like later
789 if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "video/nsv") || !strcmp(field_data
, "misc/ultravox")))
790 *file_format
= DEMUXER_TYPE_NSV
;
791 else if ( (field_data
= http_get_field(http_hdr
, "content-type")) != NULL
&& (!strcmp(field_data
, "audio/aacp") || !strcmp(field_data
, "audio/aac")))
792 *file_format
= DEMUXER_TYPE_AAC
;
794 *file_format
= DEMUXER_TYPE_AUDIO
;
798 case 400: // Server Full
799 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server is full, skipping!\n");
801 case 401: // Service Unavailable
802 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return service unavailable, skipping!\n");
804 case 403: // Service Forbidden
805 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server return 'Service Forbidden'\n");
807 case 404: // Resource Not Found
808 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: ICY-Server couldn't find requested stream, skipping!\n");
811 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
816 // Assume standard http if not ICY
817 switch( http_hdr
->status_code
) {
819 // Look if we can use the Content-Type
820 content_type
= http_get_field( http_hdr
, "Content-Type" );
821 if( content_type
!=NULL
) {
822 char *content_length
= NULL
;
823 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", content_type
);
824 if( (content_length
= http_get_field(http_hdr
, "Content-Length")) != NULL
) {
825 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length"));
826 stream
->end_pos
= atoi(content_length
);
828 // Check in the mime type table for a demuxer type
830 while(mime_type_table
[i
].mime_type
!= NULL
) {
831 if( !strcasecmp( content_type
, mime_type_table
[i
].mime_type
) ) {
832 *file_format
= mime_type_table
[i
].demuxer_type
;
839 // Not found in the mime type table, don't fail,
840 // we should try raw HTTP
844 case 301: // Permanently
845 case 302: // Temporarily
846 case 303: // See Other
847 // TODO: RFC 2616, recommand to detect infinite redirection loops
848 next_url
= http_get_field( http_hdr
, "Location" );
849 if( next_url
!=NULL
) {
850 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
851 stream
->streaming_ctrl
->url
= url_redirect( &url
, next_url
);
852 if (!strcasecmp(url
->protocol
, "mms")) {
853 res
= STREAM_REDIRECTED
;
856 if (strcasecmp(url
->protocol
, "http")) {
857 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Unsupported http %d redirect to %s protocol\n", http_hdr
->status_code
, url
->protocol
);
862 url
->protocol
= strdup("unsv");
867 case 401: // Authentication required
868 if( http_authenticate(http_hdr
, url
, &auth_retry
)<0 )
873 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
879 if (fd
> 0) closesocket( fd
);
881 http_free( http_hdr
);
884 stream
->streaming_ctrl
->data
= (void*)http_hdr
;
889 static int fixup_open(stream_t
*stream
,int seekable
) {
890 HTTP_header_t
*http_hdr
= stream
->streaming_ctrl
->data
;
891 int is_icy
= http_hdr
&& http_get_field(http_hdr
, "Icy-MetaInt");
892 int is_ultravox
= strcasecmp(stream
->streaming_ctrl
->url
->protocol
, "unsv") == 0;
894 stream
->type
= STREAMTYPE_STREAM
;
895 if(!is_icy
&& !is_ultravox
&& seekable
)
897 stream
->flags
|= MP_STREAM_SEEK
;
898 stream
->seek
= http_seek
;
900 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
901 if ((!is_icy
&& !is_ultravox
) || scast_streaming_start(stream
))
902 if(nop_streaming_start( stream
)) {
903 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_start failed\n");
905 closesocket(stream
->fd
);
907 streaming_ctrl_free(stream
->streaming_ctrl
);
908 stream
->streaming_ctrl
= NULL
;
909 return STREAM_UNSUPPORTED
;
912 fixup_network_stream_cache(stream
);
916 static int open_s1(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
920 stream
->streaming_ctrl
= streaming_ctrl_new();
921 if( stream
->streaming_ctrl
==NULL
) {
924 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
925 url
= url_new(stream
->url
);
926 stream
->streaming_ctrl
->url
= check4proxies(url
);
929 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(1), URL: %s\n", stream
->url
);
930 seekable
= http_streaming_start(stream
, file_format
);
931 if((seekable
< 0) || (*file_format
== DEMUXER_TYPE_ASF
)) {
933 closesocket(stream
->fd
);
935 if (seekable
== STREAM_REDIRECTED
)
937 streaming_ctrl_free(stream
->streaming_ctrl
);
938 stream
->streaming_ctrl
= NULL
;
939 return STREAM_UNSUPPORTED
;
942 return fixup_open(stream
, seekable
);
945 static int open_s2(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
949 stream
->streaming_ctrl
= streaming_ctrl_new();
950 if( stream
->streaming_ctrl
==NULL
) {
953 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
954 url
= url_new(stream
->url
);
955 stream
->streaming_ctrl
->url
= check4proxies(url
);
958 mp_msg(MSGT_OPEN
, MSGL_V
, "STREAM_HTTP(2), URL: %s\n", stream
->url
);
959 seekable
= http_streaming_start(stream
, file_format
);
962 closesocket(stream
->fd
);
964 streaming_ctrl_free(stream
->streaming_ctrl
);
965 stream
->streaming_ctrl
= NULL
;
966 return STREAM_UNSUPPORTED
;
969 return fixup_open(stream
, seekable
);
973 const stream_info_t stream_info_http1
= {
976 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
979 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL
},
981 0 // Urls are an option string
984 const stream_info_t stream_info_http2
= {
987 "Bertrand, Albeu, Arpi? who?",
988 "plain http, also used as fallback for many other protocols",
990 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL
}, //all the others as fallback
992 0 // Urls are an option string