sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / stream / http.c
blobd79f7632cafbc0f6eb06dca2967c046633cd1ae1
1 /*
2 * HTTP Helper
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.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #if !HAVE_WINSOCK2_H
31 #else
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 #endif
36 #include "http.h"
37 #include "url.h"
38 #include "mp_msg.h"
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "network.h"
43 #include "help_mp.h"
46 extern const mime_struct_t mime_type_table[];
47 extern int stream_cache_size;
48 extern int network_bandwidth;
50 typedef struct {
51 unsigned metaint;
52 unsigned metapos;
53 int is_ultravox;
54 } scast_data_t;
56 /**
57 * \brief first read any data from sc->buffer then from fd
58 * \param fd file descriptor to read data from
59 * \param buffer buffer to read into
60 * \param len how many bytes to read
61 * \param sc streaming control containing buffer to read from first
62 * \return len unless there is a read error or eof
64 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
65 unsigned pos = 0;
66 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
67 if (cp_len > len)
68 cp_len = len;
69 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
70 sc->buffer_pos += cp_len;
71 pos += cp_len;
72 while (pos < len) {
73 int ret = recv(fd, &buffer[pos], len - pos, 0);
74 if (ret <= 0)
75 break;
76 pos += ret;
78 return pos;
81 /**
82 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
83 * \param fd file descriptor to read from
84 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
85 * \return number of real data before next metadata block starts or 0 on error
87 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
88 unsigned metaint;
89 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
90 int info_read;
91 do {
92 info_read = my_read(fd, info, 1, sc);
93 if (info[0] == 0x00)
94 info_read = my_read(fd, info, 6, sc);
95 else
96 info_read += my_read(fd, &info[1], 5, sc);
97 if (info_read != 6) // read error or eof
98 return 0;
99 // sync byte and reserved flags
100 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
101 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
102 return 0;
104 if (info[1] & 0x01)
105 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
106 metaint = info[4] << 8 | info[5];
107 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
108 char *metabuf = malloc(metaint);
109 my_read(fd, metabuf, metaint, sc);
110 free(metabuf);
112 } while ((info[3] & 0xf) < 0x07);
113 return metaint;
117 * \brief read one scast meta data entry and print it
118 * \param fd file descriptor to read from
119 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
121 static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
122 unsigned char tmp = 0;
123 unsigned metalen;
124 my_read(fd, &tmp, 1, sc);
125 metalen = tmp * 16;
126 if (metalen > 0) {
127 char *info = malloc(metalen + 1);
128 unsigned nlen = my_read(fd, info, metalen, sc);
129 info[nlen] = 0;
130 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
131 free(info);
136 * \brief read data from scast/ultravox stream without any metadata
137 * \param fd file descriptor to read from
138 * \param buffer buffer to read data into
139 * \param size number of bytes to read
140 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
142 static int scast_streaming_read(int fd, char *buffer, int size,
143 streaming_ctrl_t *sc) {
144 scast_data_t *sd = (scast_data_t *)sc->data;
145 unsigned block, ret;
146 unsigned done = 0;
148 // first read remaining data up to next metadata
149 block = sd->metaint - sd->metapos;
150 if (block > size)
151 block = size;
152 ret = my_read(fd, buffer, block, sc);
153 sd->metapos += ret;
154 done += ret;
155 if (ret != block) // read problems or eof
156 size = done;
158 while (done < size) { // now comes the metadata
159 if (sd->is_ultravox)
161 sd->metaint = uvox_meta_read(fd, sc);
162 if (!sd->metaint)
163 size = done;
165 else
166 scast_meta_read(fd, sc); // read and display metadata
167 sd->metapos = 0;
168 block = size - done;
169 if (block > sd->metaint)
170 block = sd->metaint;
171 ret = my_read(fd, &buffer[done], block, sc);
172 sd->metapos += ret;
173 done += ret;
174 if (ret != block) // read problems or eof
175 size = done;
177 return done;
180 static int scast_streaming_start(stream_t *stream) {
181 int metaint;
182 scast_data_t *scast_data;
183 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
184 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
185 if (!stream || stream->fd < 0 || !http_hdr)
186 return -1;
187 if (is_ultravox)
188 metaint = 0;
189 else {
190 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
191 if (metaint <= 0)
192 return -1;
194 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
195 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
196 stream->streaming_ctrl->buffer_pos = 0;
197 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
198 scast_data = malloc(sizeof(scast_data_t));
199 scast_data->metaint = metaint;
200 scast_data->metapos = 0;
201 scast_data->is_ultravox = is_ultravox;
202 http_free(http_hdr);
203 stream->streaming_ctrl->data = scast_data;
204 stream->streaming_ctrl->streaming_read = scast_streaming_read;
205 stream->streaming_ctrl->streaming_seek = NULL;
206 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
207 stream->streaming_ctrl->buffering = 1;
208 stream->streaming_ctrl->status = streaming_playing_e;
209 return 0;
212 static int nop_streaming_start( stream_t *stream ) {
213 HTTP_header_t *http_hdr = NULL;
214 char *next_url=NULL;
215 URL_t *rd_url=NULL;
216 int fd,ret;
217 if( stream==NULL ) return -1;
219 fd = stream->fd;
220 if( fd<0 ) {
221 fd = http_send_request( stream->streaming_ctrl->url, 0 );
222 if( fd<0 ) return -1;
223 http_hdr = http_read_response( fd );
224 if( http_hdr==NULL ) return -1;
226 switch( http_hdr->status_code ) {
227 case 200: // OK
228 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
229 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
230 if( http_hdr->body_size>0 ) {
231 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
232 http_free( http_hdr );
233 return -1;
236 break;
237 // Redirect
238 case 301: // Permanently
239 case 302: // Temporarily
240 case 303: // See Other
241 ret=-1;
242 next_url = http_get_field( http_hdr, "Location" );
244 if (next_url != NULL)
245 rd_url=url_new(next_url);
247 if (next_url != NULL && rd_url != NULL) {
248 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
249 stream->streaming_ctrl->url=check4proxies(rd_url);
250 ret=nop_streaming_start(stream); //recursively get streaming started
251 } else {
252 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
253 closesocket( fd );
254 fd = -1;
256 return ret;
257 break;
258 case 401: //Authorization required
259 case 403: //Forbidden
260 case 404: //Not found
261 case 500: //Server Error
262 default:
263 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
264 closesocket( fd );
265 fd = -1;
266 return -1;
267 break;
269 stream->fd = fd;
270 } else {
271 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
272 if( http_hdr->body_size>0 ) {
273 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
274 http_free( http_hdr );
275 stream->streaming_ctrl->data = NULL;
276 return -1;
281 if( http_hdr ) {
282 http_free( http_hdr );
283 stream->streaming_ctrl->data = NULL;
286 stream->streaming_ctrl->streaming_read = nop_streaming_read;
287 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
288 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
289 stream->streaming_ctrl->buffering = 1;
290 stream->streaming_ctrl->status = streaming_playing_e;
291 return 0;
294 HTTP_header_t *
295 http_new_header(void) {
296 HTTP_header_t *http_hdr;
298 http_hdr = malloc(sizeof(HTTP_header_t));
299 if( http_hdr==NULL ) return NULL;
300 memset( http_hdr, 0, sizeof(HTTP_header_t) );
302 return http_hdr;
305 void
306 http_free( HTTP_header_t *http_hdr ) {
307 HTTP_field_t *field, *field2free;
308 if( http_hdr==NULL ) return;
309 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
310 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
311 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
312 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
313 if( http_hdr->method!=NULL ) free( http_hdr->method );
314 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
315 field = http_hdr->first_field;
316 while( field!=NULL ) {
317 field2free = field;
318 if (field->field_name)
319 free(field->field_name);
320 field = field->next;
321 free( field2free );
323 free( http_hdr );
324 http_hdr = NULL;
328 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
329 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
331 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
332 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
333 return -1;
335 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
336 if( http_hdr->buffer==NULL ) {
337 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
338 return -1;
340 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
341 http_hdr->buffer_size += length;
342 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
343 return http_hdr->buffer_size;
347 http_is_header_entire( HTTP_header_t *http_hdr ) {
348 if( http_hdr==NULL ) return -1;
349 if( http_hdr->buffer==NULL ) return 0; // empty
351 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
352 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
353 return 1;
357 http_response_parse( HTTP_header_t *http_hdr ) {
358 char *hdr_ptr, *ptr;
359 char *field=NULL;
360 int pos_hdr_sep, hdr_sep_len;
361 size_t len;
362 if( http_hdr==NULL ) return -1;
363 if( http_hdr->is_parsed ) return 0;
365 // Get the protocol
366 hdr_ptr = strstr( http_hdr->buffer, " " );
367 if( hdr_ptr==NULL ) {
368 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
369 return -1;
371 len = hdr_ptr-http_hdr->buffer;
372 http_hdr->protocol = malloc(len+1);
373 if( http_hdr->protocol==NULL ) {
374 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
375 return -1;
377 strncpy( http_hdr->protocol, http_hdr->buffer, len );
378 http_hdr->protocol[len]='\0';
379 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
380 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
381 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
382 return -1;
386 // Get the status code
387 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
388 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
389 return -1;
391 hdr_ptr += 4;
393 // Get the reason phrase
394 ptr = strstr( hdr_ptr, "\n" );
395 if( hdr_ptr==NULL ) {
396 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
397 return -1;
399 len = ptr-hdr_ptr;
400 http_hdr->reason_phrase = malloc(len+1);
401 if( http_hdr->reason_phrase==NULL ) {
402 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
403 return -1;
405 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
406 if( http_hdr->reason_phrase[len-1]=='\r' ) {
407 len--;
409 http_hdr->reason_phrase[len]='\0';
411 // Set the position of the header separator: \r\n\r\n
412 hdr_sep_len = 4;
413 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
414 if( ptr==NULL ) {
415 ptr = strstr( http_hdr->buffer, "\n\n" );
416 if( ptr==NULL ) {
417 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
418 return -1;
420 hdr_sep_len = 2;
422 pos_hdr_sep = ptr-http_hdr->buffer;
424 // Point to the first line after the method line.
425 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
426 do {
427 ptr = hdr_ptr;
428 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
429 len = ptr-hdr_ptr;
430 if( len==0 ) break;
431 field = realloc(field, len+1);
432 if( field==NULL ) {
433 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
434 return -1;
436 strncpy( field, hdr_ptr, len );
437 field[len]='\0';
438 http_set_field( http_hdr, field );
439 hdr_ptr = ptr+((*ptr=='\r')?2:1);
440 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
442 if( field!=NULL ) free( field );
444 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
445 // Response has data!
446 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
447 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
450 http_hdr->is_parsed = 1;
451 return 0;
454 char *
455 http_build_request( HTTP_header_t *http_hdr ) {
456 char *ptr, *uri=NULL;
457 int len;
458 HTTP_field_t *field;
459 if( http_hdr==NULL ) return NULL;
461 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
462 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
463 else {
464 uri = malloc(strlen(http_hdr->uri) + 1);
465 if( uri==NULL ) {
466 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
467 return NULL;
469 strcpy(uri,http_hdr->uri);
472 //**** Compute the request length
473 // Add the Method line
474 len = strlen(http_hdr->method)+strlen(uri)+12;
475 // Add the fields
476 field = http_hdr->first_field;
477 while( field!=NULL ) {
478 len += strlen(field->field_name)+2;
479 field = field->next;
481 // Add the CRLF
482 len += 2;
483 // Add the body
484 if( http_hdr->body!=NULL ) {
485 len += http_hdr->body_size;
487 // Free the buffer if it was previously used
488 if( http_hdr->buffer!=NULL ) {
489 free( http_hdr->buffer );
490 http_hdr->buffer = NULL;
492 http_hdr->buffer = malloc(len+1);
493 if( http_hdr->buffer==NULL ) {
494 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
495 return NULL;
497 http_hdr->buffer_size = len;
499 //*** Building the request
500 ptr = http_hdr->buffer;
501 // Add the method line
502 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
503 field = http_hdr->first_field;
504 // Add the field
505 while( field!=NULL ) {
506 ptr += sprintf( ptr, "%s\r\n", field->field_name );
507 field = field->next;
509 ptr += sprintf( ptr, "\r\n" );
510 // Add the body
511 if( http_hdr->body!=NULL ) {
512 memcpy( ptr, http_hdr->body, http_hdr->body_size );
515 if( uri ) free( uri );
516 return http_hdr->buffer;
519 char *
520 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
521 if( http_hdr==NULL || field_name==NULL ) return NULL;
522 http_hdr->field_search_pos = http_hdr->first_field;
523 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 );
524 if( http_hdr->field_search==NULL ) {
525 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
526 return NULL;
528 strcpy( http_hdr->field_search, field_name );
529 return http_get_next_field( http_hdr );
532 char *
533 http_get_next_field( HTTP_header_t *http_hdr ) {
534 char *ptr;
535 HTTP_field_t *field;
536 if( http_hdr==NULL ) return NULL;
538 field = http_hdr->field_search_pos;
539 while( field!=NULL ) {
540 ptr = strstr( field->field_name, ":" );
541 if( ptr==NULL ) return NULL;
542 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
543 ptr++; // Skip the column
544 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
545 http_hdr->field_search_pos = field->next;
546 return ptr; // return the value without the field name
548 field = field->next;
550 return NULL;
553 void
554 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
555 HTTP_field_t *new_field;
556 if( http_hdr==NULL || field_name==NULL ) return;
558 new_field = malloc(sizeof(HTTP_field_t));
559 if( new_field==NULL ) {
560 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
561 return;
563 new_field->next = NULL;
564 new_field->field_name = malloc(strlen(field_name)+1);
565 if( new_field->field_name==NULL ) {
566 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
567 free(new_field);
568 return;
570 strcpy( new_field->field_name, field_name );
572 if( http_hdr->last_field==NULL ) {
573 http_hdr->first_field = new_field;
574 } else {
575 http_hdr->last_field->next = new_field;
577 http_hdr->last_field = new_field;
578 http_hdr->field_nb++;
581 void
582 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
583 if( http_hdr==NULL || method==NULL ) return;
585 http_hdr->method = malloc(strlen(method)+1);
586 if( http_hdr->method==NULL ) {
587 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
588 return;
590 strcpy( http_hdr->method, method );
593 void
594 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
595 if( http_hdr==NULL || uri==NULL ) return;
597 http_hdr->uri = malloc(strlen(uri)+1);
598 if( http_hdr->uri==NULL ) {
599 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
600 return;
602 strcpy( http_hdr->uri, uri );
606 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
607 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
608 int encoded_len, pass_len=0, out_len;
609 int res = -1;
610 if( http_hdr==NULL || username==NULL ) return -1;
612 if( password!=NULL ) {
613 pass_len = strlen(password);
616 usr_pass = malloc(strlen(username)+pass_len+2);
617 if( usr_pass==NULL ) {
618 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
619 goto out;
622 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
624 // Base 64 encode with at least 33% more data than the original size
625 encoded_len = strlen(usr_pass)*2;
626 b64_usr_pass = malloc(encoded_len);
627 if( b64_usr_pass==NULL ) {
628 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
629 goto out;
632 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
633 if( out_len<0 ) {
634 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
635 goto out;
638 b64_usr_pass[out_len]='\0';
640 auth = malloc(encoded_len+22);
641 if( auth==NULL ) {
642 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
643 goto out;
646 sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
647 http_set_field( http_hdr, auth );
648 res = 0;
650 out:
651 free( usr_pass );
652 free( b64_usr_pass );
653 free( auth );
655 return res;
658 void
659 http_debug_hdr( HTTP_header_t *http_hdr ) {
660 HTTP_field_t *field;
661 int i = 0;
662 if( http_hdr==NULL ) return;
664 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
665 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
666 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
667 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
668 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
669 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
670 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
671 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size );
673 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
674 field = http_hdr->first_field;
675 while( field!=NULL ) {
676 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
677 field = field->next;
679 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
683 base64_encode(const void *enc, int encLen, char *out, int outMax) {
684 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
686 unsigned char *encBuf;
687 int outLen;
688 unsigned int bits;
689 unsigned int shift;
691 encBuf = (unsigned char*)enc;
692 outLen = 0;
693 bits = 0;
694 shift = 0;
695 outMax &= ~3;
697 while(1) {
698 if( encLen>0 ) {
699 // Shift in byte
700 bits <<= 8;
701 bits |= *encBuf;
702 shift += 8;
703 // Next byte
704 encBuf++;
705 encLen--;
706 } else if( shift>0 ) {
707 // Pad last bits to 6 bits - will end next loop
708 bits <<= 6 - shift;
709 shift = 6;
710 } else {
711 // As per RFC 2045, section 6.8,
712 // pad output as necessary: 0 to 2 '=' chars.
713 while( outLen & 3 ){
714 *out++ = '=';
715 outLen++;
718 return outLen;
721 // Encode 6 bit segments
722 while( shift>=6 ) {
723 if (outLen >= outMax)
724 return -1;
725 shift -= 6;
726 *out = b64[ (bits >> shift) & 0x3F ];
727 out++;
728 outLen++;
733 static void print_icy_metadata(HTTP_header_t *http_hdr) {
734 const char *field_data;
735 // note: I skip icy-notice1 and 2, as they contain html <BR>
736 // and are IMHO useless info ::atmos
737 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
738 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
739 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
740 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
741 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
742 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
743 // XXX: does this really mean public server? ::atmos
744 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
745 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
746 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
747 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
750 //! If this function succeeds you must closesocket stream->fd
751 static int http_streaming_start(stream_t *stream, int* file_format) {
752 HTTP_header_t *http_hdr = NULL;
753 unsigned int i;
754 int fd = stream->fd;
755 int res = STREAM_UNSUPPORTED;
756 int redirect = 0;
757 int auth_retry=0;
758 int seekable=0;
759 char *content_type;
760 const char *content_length;
761 char *next_url;
762 URL_t *url = stream->streaming_ctrl->url;
766 redirect = 0;
767 if (fd > 0) closesocket(fd);
768 fd = http_send_request( url, 0 );
769 if( fd<0 ) {
770 goto err_out;
773 http_free(http_hdr);
774 http_hdr = http_read_response( fd );
775 if( http_hdr==NULL ) {
776 goto err_out;
779 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
780 http_debug_hdr( http_hdr );
783 // Check if we can make partial content requests and thus seek in http-streams
784 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
785 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
786 const char *server = http_get_field(http_hdr, "Server");
787 if (accept_ranges)
788 seekable = strncmp(accept_ranges,"bytes",5)==0;
789 else if (server && strcmp(server, "gvs 1.0") == 0)
790 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
793 print_icy_metadata(http_hdr);
795 // Check if the response is an ICY status_code reason_phrase
796 if( !strcasecmp(http_hdr->protocol, "ICY") ||
797 http_get_field(http_hdr, "Icy-MetaInt") ) {
798 switch( http_hdr->status_code ) {
799 case 200: { // OK
800 char *field_data;
801 // If content-type == video/nsv we most likely have a winamp video stream
802 // otherwise it should be mp3. if there are more types consider adding mime type
803 // handling like later
804 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
805 *file_format = DEMUXER_TYPE_NSV;
806 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
807 *file_format = DEMUXER_TYPE_AAC;
808 else
809 *file_format = DEMUXER_TYPE_AUDIO;
810 res = STREAM_ERROR;
811 goto out;
813 case 400: // Server Full
814 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
815 goto err_out;
816 case 401: // Service Unavailable
817 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
818 goto err_out;
819 case 403: // Service Forbidden
820 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
821 goto err_out;
822 case 404: // Resource Not Found
823 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
824 goto err_out;
825 default:
826 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
827 goto err_out;
831 // Assume standard http if not ICY
832 switch( http_hdr->status_code ) {
833 case 200: // OK
834 content_length = http_get_field(http_hdr, "Content-Length");
835 if (content_length) {
836 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
837 stream->end_pos = atoll(content_length);
839 // Look if we can use the Content-Type
840 content_type = http_get_field( http_hdr, "Content-Type" );
841 if( content_type!=NULL ) {
842 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
843 // Check in the mime type table for a demuxer type
844 i = 0;
845 while(mime_type_table[i].mime_type != NULL) {
846 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
847 *file_format = mime_type_table[i].demuxer_type;
848 res = seekable;
849 goto out;
851 i++;
854 // Not found in the mime type table, don't fail,
855 // we should try raw HTTP
856 res = seekable;
857 goto out;
858 // Redirect
859 case 301: // Permanently
860 case 302: // Temporarily
861 case 303: // See Other
862 // TODO: RFC 2616, recommand to detect infinite redirection loops
863 next_url = http_get_field( http_hdr, "Location" );
864 if( next_url!=NULL ) {
865 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
866 stream->streaming_ctrl->url = url_redirect( &url, next_url );
867 if (!strcasecmp(url->protocol, "mms")) {
868 res = STREAM_REDIRECTED;
869 goto err_out;
871 if (strcasecmp(url->protocol, "http")) {
872 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
873 goto err_out;
875 if (is_ultravox) {
876 free(url->protocol);
877 url->protocol = strdup("unsv");
879 redirect = 1;
881 break;
882 case 401: // Authentication required
883 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
884 goto err_out;
885 redirect = 1;
886 break;
887 default:
888 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
889 goto err_out;
891 } while( redirect );
893 err_out:
894 if (fd > 0) closesocket( fd );
895 fd = -1;
896 http_free( http_hdr );
897 http_hdr = NULL;
898 out:
899 stream->streaming_ctrl->data = (void*)http_hdr;
900 stream->fd = fd;
901 return res;
904 static int fixup_open(stream_t *stream,int seekable) {
905 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
906 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
907 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
909 stream->type = STREAMTYPE_STREAM;
910 if(!is_icy && !is_ultravox && seekable)
912 stream->flags |= MP_STREAM_SEEK;
913 stream->seek = http_seek;
915 stream->streaming_ctrl->bandwidth = network_bandwidth;
916 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
917 if(nop_streaming_start( stream )) {
918 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
919 if (stream->fd >= 0)
920 closesocket(stream->fd);
921 stream->fd = -1;
922 streaming_ctrl_free(stream->streaming_ctrl);
923 stream->streaming_ctrl = NULL;
924 return STREAM_UNSUPPORTED;
927 fixup_network_stream_cache(stream);
928 return STREAM_OK;
931 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
932 int seekable=0;
933 URL_t *url;
935 stream->streaming_ctrl = streaming_ctrl_new();
936 if( stream->streaming_ctrl==NULL ) {
937 return STREAM_ERROR;
939 stream->streaming_ctrl->bandwidth = network_bandwidth;
940 url = url_new(stream->url);
941 stream->streaming_ctrl->url = check4proxies(url);
942 url_free(url);
944 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
945 seekable = http_streaming_start(stream, file_format);
946 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
947 if (stream->fd >= 0)
948 closesocket(stream->fd);
949 stream->fd = -1;
950 if (seekable == STREAM_REDIRECTED)
951 return seekable;
952 streaming_ctrl_free(stream->streaming_ctrl);
953 stream->streaming_ctrl = NULL;
954 return STREAM_UNSUPPORTED;
957 return fixup_open(stream, seekable);
960 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
961 int seekable=0;
962 URL_t *url;
964 stream->streaming_ctrl = streaming_ctrl_new();
965 if( stream->streaming_ctrl==NULL ) {
966 return STREAM_ERROR;
968 stream->streaming_ctrl->bandwidth = network_bandwidth;
969 url = url_new(stream->url);
970 stream->streaming_ctrl->url = check4proxies(url);
971 url_free(url);
973 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
974 seekable = http_streaming_start(stream, file_format);
975 if(seekable < 0) {
976 if (stream->fd >= 0)
977 closesocket(stream->fd);
978 stream->fd = -1;
979 streaming_ctrl_free(stream->streaming_ctrl);
980 stream->streaming_ctrl = NULL;
981 return STREAM_UNSUPPORTED;
984 return fixup_open(stream, seekable);
988 const stream_info_t stream_info_http1 = {
989 "http streaming",
990 "null",
991 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
992 "plain http",
993 open_s1,
994 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
995 NULL,
996 0 // Urls are an option string
999 const stream_info_t stream_info_http2 = {
1000 "http streaming",
1001 "null",
1002 "Bertrand, Albeu, Arpi? who?",
1003 "plain http, also used as fallback for many other protocols",
1004 open_s2,
1005 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
1006 NULL,
1007 0 // Urls are an option string