Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / stream / http.c
blob3a81f0accf974f9d691e836a73762d8c8b8a05ea
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 int i;
128 uint8_t *info = malloc(metalen + 1);
129 unsigned nlen = my_read(fd, info, metalen, sc);
130 // avoid breaking the user's terminal too much
131 if (nlen > 256) nlen = 256;
132 for (i = 0; i < nlen; i++)
133 if (info[i] && info[i] < 32) info[i] = '?';
134 info[nlen] = 0;
135 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
136 free(info);
141 * \brief read data from scast/ultravox stream without any metadata
142 * \param fd file descriptor to read from
143 * \param buffer buffer to read data into
144 * \param size number of bytes to read
145 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
147 static int scast_streaming_read(int fd, char *buffer, int size,
148 streaming_ctrl_t *sc) {
149 scast_data_t *sd = (scast_data_t *)sc->data;
150 unsigned block, ret;
151 unsigned done = 0;
153 // first read remaining data up to next metadata
154 block = sd->metaint - sd->metapos;
155 if (block > size)
156 block = size;
157 ret = my_read(fd, buffer, block, sc);
158 sd->metapos += ret;
159 done += ret;
160 if (ret != block) // read problems or eof
161 size = done;
163 while (done < size) { // now comes the metadata
164 if (sd->is_ultravox)
166 sd->metaint = uvox_meta_read(fd, sc);
167 if (!sd->metaint)
168 size = done;
170 else
171 scast_meta_read(fd, sc); // read and display metadata
172 sd->metapos = 0;
173 block = size - done;
174 if (block > sd->metaint)
175 block = sd->metaint;
176 ret = my_read(fd, &buffer[done], block, sc);
177 sd->metapos += ret;
178 done += ret;
179 if (ret != block) // read problems or eof
180 size = done;
182 return done;
185 static int scast_streaming_start(stream_t *stream) {
186 int metaint;
187 scast_data_t *scast_data;
188 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
189 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
190 if (!stream || stream->fd < 0 || !http_hdr)
191 return -1;
192 if (is_ultravox)
193 metaint = 0;
194 else {
195 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
196 if (metaint <= 0)
197 return -1;
199 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
200 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
201 stream->streaming_ctrl->buffer_pos = 0;
202 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
203 scast_data = malloc(sizeof(scast_data_t));
204 scast_data->metaint = metaint;
205 scast_data->metapos = 0;
206 scast_data->is_ultravox = is_ultravox;
207 http_free(http_hdr);
208 stream->streaming_ctrl->data = scast_data;
209 stream->streaming_ctrl->streaming_read = scast_streaming_read;
210 stream->streaming_ctrl->streaming_seek = NULL;
211 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
212 stream->streaming_ctrl->buffering = 1;
213 stream->streaming_ctrl->status = streaming_playing_e;
214 return 0;
217 static int nop_streaming_start( stream_t *stream ) {
218 HTTP_header_t *http_hdr = NULL;
219 char *next_url=NULL;
220 URL_t *rd_url=NULL;
221 int fd,ret;
222 if( stream==NULL ) return -1;
224 fd = stream->fd;
225 if( fd<0 ) {
226 fd = http_send_request( stream->streaming_ctrl->url, 0 );
227 if( fd<0 ) return -1;
228 http_hdr = http_read_response( fd );
229 if( http_hdr==NULL ) return -1;
231 switch( http_hdr->status_code ) {
232 case 200: // OK
233 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
234 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
235 if( http_hdr->body_size>0 ) {
236 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
237 http_free( http_hdr );
238 return -1;
241 break;
242 // Redirect
243 case 301: // Permanently
244 case 302: // Temporarily
245 case 303: // See Other
246 ret=-1;
247 next_url = http_get_field( http_hdr, "Location" );
249 if (next_url != NULL)
250 rd_url=url_new(next_url);
252 if (next_url != NULL && rd_url != NULL) {
253 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
254 stream->streaming_ctrl->url=check4proxies(rd_url);
255 ret=nop_streaming_start(stream); //recursively get streaming started
256 } else {
257 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
258 closesocket( fd );
259 fd = -1;
261 return ret;
262 break;
263 case 401: //Authorization required
264 case 403: //Forbidden
265 case 404: //Not found
266 case 500: //Server Error
267 default:
268 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
269 closesocket( fd );
270 fd = -1;
271 return -1;
272 break;
274 stream->fd = fd;
275 } else {
276 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
277 if( http_hdr->body_size>0 ) {
278 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
279 http_free( http_hdr );
280 stream->streaming_ctrl->data = NULL;
281 return -1;
286 if( http_hdr ) {
287 http_free( http_hdr );
288 stream->streaming_ctrl->data = NULL;
291 stream->streaming_ctrl->streaming_read = nop_streaming_read;
292 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
293 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
294 stream->streaming_ctrl->buffering = 1;
295 stream->streaming_ctrl->status = streaming_playing_e;
296 return 0;
299 HTTP_header_t *
300 http_new_header(void) {
301 HTTP_header_t *http_hdr;
303 http_hdr = malloc(sizeof(HTTP_header_t));
304 if( http_hdr==NULL ) return NULL;
305 memset( http_hdr, 0, sizeof(HTTP_header_t) );
307 return http_hdr;
310 void
311 http_free( HTTP_header_t *http_hdr ) {
312 HTTP_field_t *field, *field2free;
313 if( http_hdr==NULL ) return;
314 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
315 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
316 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
317 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
318 if( http_hdr->method!=NULL ) free( http_hdr->method );
319 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
320 field = http_hdr->first_field;
321 while( field!=NULL ) {
322 field2free = field;
323 if (field->field_name)
324 free(field->field_name);
325 field = field->next;
326 free( field2free );
328 free( http_hdr );
329 http_hdr = NULL;
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");
338 return -1;
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");
343 return -1;
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;
358 return 1;
362 http_response_parse( HTTP_header_t *http_hdr ) {
363 char *hdr_ptr, *ptr;
364 char *field=NULL;
365 int pos_hdr_sep, hdr_sep_len;
366 size_t len;
367 if( http_hdr==NULL ) return -1;
368 if( http_hdr->is_parsed ) return 0;
370 // Get the protocol
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");
374 return -1;
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");
380 return -1;
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");
387 return -1;
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");
394 return -1;
396 hdr_ptr += 4;
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");
402 return -1;
404 len = ptr-hdr_ptr;
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");
408 return -1;
410 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
411 if( http_hdr->reason_phrase[len-1]=='\r' ) {
412 len--;
414 http_hdr->reason_phrase[len]='\0';
416 // Set the position of the header separator: \r\n\r\n
417 hdr_sep_len = 4;
418 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
419 if( ptr==NULL ) {
420 ptr = strstr( http_hdr->buffer, "\n\n" );
421 if( ptr==NULL ) {
422 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
423 return -1;
425 hdr_sep_len = 2;
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;
431 do {
432 ptr = hdr_ptr;
433 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
434 len = ptr-hdr_ptr;
435 if( len==0 ) break;
436 field = realloc(field, len+1);
437 if( field==NULL ) {
438 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
439 return -1;
441 strncpy( field, hdr_ptr, len );
442 field[len]='\0';
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) );
447 if( field!=NULL ) free( field );
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;
456 return 0;
459 char *
460 http_build_request( HTTP_header_t *http_hdr ) {
461 char *ptr, *uri=NULL;
462 int len;
463 HTTP_field_t *field;
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, "/");
468 else {
469 uri = malloc(strlen(http_hdr->uri) + 1);
470 if( uri==NULL ) {
471 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
472 return NULL;
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;
480 // Add the fields
481 field = http_hdr->first_field;
482 while( field!=NULL ) {
483 len += strlen(field->field_name)+2;
484 field = field->next;
486 // Add the CRLF
487 len += 2;
488 // Add the body
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");
500 return NULL;
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;
509 // Add the field
510 while( field!=NULL ) {
511 ptr += sprintf( ptr, "%s\r\n", field->field_name );
512 field = field->next;
514 ptr += sprintf( ptr, "\r\n" );
515 // Add the body
516 if( http_hdr->body!=NULL ) {
517 memcpy( ptr, http_hdr->body, http_hdr->body_size );
520 if( uri ) free( uri );
521 return http_hdr->buffer;
524 char *
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");
531 return NULL;
533 strcpy( http_hdr->field_search, field_name );
534 return http_get_next_field( http_hdr );
537 char *
538 http_get_next_field( HTTP_header_t *http_hdr ) {
539 char *ptr;
540 HTTP_field_t *field;
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
553 field = field->next;
555 return NULL;
558 void
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");
566 return;
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");
572 free(new_field);
573 return;
575 strcpy( new_field->field_name, field_name );
577 if( http_hdr->last_field==NULL ) {
578 http_hdr->first_field = new_field;
579 } else {
580 http_hdr->last_field->next = new_field;
582 http_hdr->last_field = new_field;
583 http_hdr->field_nb++;
586 void
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");
593 return;
595 strcpy( http_hdr->method, method );
598 void
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");
605 return;
607 strcpy( http_hdr->uri, uri );
611 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
612 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
613 int encoded_len, pass_len=0, out_len;
614 int res = -1;
615 if( http_hdr==NULL || username==NULL ) return -1;
617 if( password!=NULL ) {
618 pass_len = strlen(password);
621 usr_pass = malloc(strlen(username)+pass_len+2);
622 if( usr_pass==NULL ) {
623 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
624 goto out;
627 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
629 // Base 64 encode with at least 33% more data than the original size
630 encoded_len = strlen(usr_pass)*2;
631 b64_usr_pass = malloc(encoded_len);
632 if( b64_usr_pass==NULL ) {
633 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
634 goto out;
637 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
638 if( out_len<0 ) {
639 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
640 goto out;
643 b64_usr_pass[out_len]='\0';
645 auth = malloc(encoded_len+22);
646 if( auth==NULL ) {
647 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
648 goto out;
651 sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
652 http_set_field( http_hdr, auth );
653 res = 0;
655 out:
656 free( usr_pass );
657 free( b64_usr_pass );
658 free( auth );
660 return res;
663 void
664 http_debug_hdr( HTTP_header_t *http_hdr ) {
665 HTTP_field_t *field;
666 int i = 0;
667 if( http_hdr==NULL ) return;
669 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
670 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
671 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
672 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
673 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
674 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
675 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
676 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size );
678 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
679 field = http_hdr->first_field;
680 while( field!=NULL ) {
681 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
682 field = field->next;
684 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
688 base64_encode(const void *enc, int encLen, char *out, int outMax) {
689 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
691 unsigned char *encBuf;
692 int outLen;
693 unsigned int bits;
694 unsigned int shift;
696 encBuf = (unsigned char*)enc;
697 outLen = 0;
698 bits = 0;
699 shift = 0;
700 outMax &= ~3;
702 while(1) {
703 if( encLen>0 ) {
704 // Shift in byte
705 bits <<= 8;
706 bits |= *encBuf;
707 shift += 8;
708 // Next byte
709 encBuf++;
710 encLen--;
711 } else if( shift>0 ) {
712 // Pad last bits to 6 bits - will end next loop
713 bits <<= 6 - shift;
714 shift = 6;
715 } else {
716 // As per RFC 2045, section 6.8,
717 // pad output as necessary: 0 to 2 '=' chars.
718 while( outLen & 3 ){
719 *out++ = '=';
720 outLen++;
723 return outLen;
726 // Encode 6 bit segments
727 while( shift>=6 ) {
728 if (outLen >= outMax)
729 return -1;
730 shift -= 6;
731 *out = b64[ (bits >> shift) & 0x3F ];
732 out++;
733 outLen++;
738 static void print_icy_metadata(HTTP_header_t *http_hdr) {
739 const char *field_data;
740 // note: I skip icy-notice1 and 2, as they contain html <BR>
741 // and are IMHO useless info ::atmos
742 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
743 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
744 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
745 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
746 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
747 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
748 // XXX: does this really mean public server? ::atmos
749 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
750 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
751 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
752 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
755 //! If this function succeeds you must closesocket stream->fd
756 static int http_streaming_start(stream_t *stream, int* file_format) {
757 HTTP_header_t *http_hdr = NULL;
758 unsigned int i;
759 int fd = stream->fd;
760 int res = STREAM_UNSUPPORTED;
761 int redirect = 0;
762 int auth_retry=0;
763 int seekable=0;
764 char *content_type;
765 const char *content_length;
766 char *next_url;
767 URL_t *url = stream->streaming_ctrl->url;
771 redirect = 0;
772 if (fd > 0) closesocket(fd);
773 fd = http_send_request( url, 0 );
774 if( fd<0 ) {
775 goto err_out;
778 http_free(http_hdr);
779 http_hdr = http_read_response( fd );
780 if( http_hdr==NULL ) {
781 goto err_out;
784 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
785 http_debug_hdr( http_hdr );
788 // Check if we can make partial content requests and thus seek in http-streams
789 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
790 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
791 const char *server = http_get_field(http_hdr, "Server");
792 if (accept_ranges)
793 seekable = strncmp(accept_ranges,"bytes",5)==0;
794 else if (server && strcmp(server, "gvs 1.0") == 0)
795 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
798 print_icy_metadata(http_hdr);
800 // Check if the response is an ICY status_code reason_phrase
801 if( !strcasecmp(http_hdr->protocol, "ICY") ||
802 http_get_field(http_hdr, "Icy-MetaInt") ) {
803 switch( http_hdr->status_code ) {
804 case 200: { // OK
805 char *field_data;
806 // If content-type == video/nsv we most likely have a winamp video stream
807 // otherwise it should be mp3. if there are more types consider adding mime type
808 // handling like later
809 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
810 *file_format = DEMUXER_TYPE_NSV;
811 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
812 *file_format = DEMUXER_TYPE_AAC;
813 else
814 *file_format = DEMUXER_TYPE_AUDIO;
815 res = STREAM_ERROR;
816 goto out;
818 case 400: // Server Full
819 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
820 goto err_out;
821 case 401: // Service Unavailable
822 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
823 goto err_out;
824 case 403: // Service Forbidden
825 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
826 goto err_out;
827 case 404: // Resource Not Found
828 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
829 goto err_out;
830 default:
831 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
832 goto err_out;
836 // Assume standard http if not ICY
837 switch( http_hdr->status_code ) {
838 case 200: // OK
839 content_length = http_get_field(http_hdr, "Content-Length");
840 if (content_length) {
841 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
842 stream->end_pos = atoll(content_length);
844 // Look if we can use the Content-Type
845 content_type = http_get_field( http_hdr, "Content-Type" );
846 if( content_type!=NULL ) {
847 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
848 // Check in the mime type table for a demuxer type
849 i = 0;
850 while(mime_type_table[i].mime_type != NULL) {
851 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
852 *file_format = mime_type_table[i].demuxer_type;
853 res = seekable;
854 goto out;
856 i++;
859 // Not found in the mime type table, don't fail,
860 // we should try raw HTTP
861 res = seekable;
862 goto out;
863 // Redirect
864 case 301: // Permanently
865 case 302: // Temporarily
866 case 303: // See Other
867 // TODO: RFC 2616, recommand to detect infinite redirection loops
868 next_url = http_get_field( http_hdr, "Location" );
869 if( next_url!=NULL ) {
870 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
871 stream->streaming_ctrl->url = url_redirect( &url, next_url );
872 if (!strcasecmp(url->protocol, "mms")) {
873 res = STREAM_REDIRECTED;
874 goto err_out;
876 if (strcasecmp(url->protocol, "http")) {
877 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
878 goto err_out;
880 if (is_ultravox) {
881 free(url->protocol);
882 url->protocol = strdup("unsv");
884 redirect = 1;
886 break;
887 case 401: // Authentication required
888 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
889 goto err_out;
890 redirect = 1;
891 break;
892 default:
893 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
894 goto err_out;
896 } while( redirect );
898 err_out:
899 if (fd > 0) closesocket( fd );
900 fd = -1;
901 http_free( http_hdr );
902 http_hdr = NULL;
903 out:
904 stream->streaming_ctrl->data = (void*)http_hdr;
905 stream->fd = fd;
906 return res;
909 static int fixup_open(stream_t *stream,int seekable) {
910 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
911 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
912 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
914 stream->type = STREAMTYPE_STREAM;
915 if(!is_icy && !is_ultravox && seekable)
917 stream->flags |= MP_STREAM_SEEK;
918 stream->seek = http_seek;
920 stream->streaming_ctrl->bandwidth = network_bandwidth;
921 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
922 if(nop_streaming_start( stream )) {
923 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
924 if (stream->fd >= 0)
925 closesocket(stream->fd);
926 stream->fd = -1;
927 streaming_ctrl_free(stream->streaming_ctrl);
928 stream->streaming_ctrl = NULL;
929 return STREAM_UNSUPPORTED;
932 fixup_network_stream_cache(stream);
933 return STREAM_OK;
936 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
937 int seekable=0;
938 URL_t *url;
940 stream->streaming_ctrl = streaming_ctrl_new();
941 if( stream->streaming_ctrl==NULL ) {
942 return STREAM_ERROR;
944 stream->streaming_ctrl->bandwidth = network_bandwidth;
945 url = url_new(stream->url);
946 stream->streaming_ctrl->url = check4proxies(url);
947 url_free(url);
949 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
950 seekable = http_streaming_start(stream, file_format);
951 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
952 if (stream->fd >= 0)
953 closesocket(stream->fd);
954 stream->fd = -1;
955 if (seekable == STREAM_REDIRECTED)
956 return seekable;
957 streaming_ctrl_free(stream->streaming_ctrl);
958 stream->streaming_ctrl = NULL;
959 return STREAM_UNSUPPORTED;
962 return fixup_open(stream, seekable);
965 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
966 int seekable=0;
967 URL_t *url;
969 stream->streaming_ctrl = streaming_ctrl_new();
970 if( stream->streaming_ctrl==NULL ) {
971 return STREAM_ERROR;
973 stream->streaming_ctrl->bandwidth = network_bandwidth;
974 url = url_new(stream->url);
975 stream->streaming_ctrl->url = check4proxies(url);
976 url_free(url);
978 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
979 seekable = http_streaming_start(stream, file_format);
980 if(seekable < 0) {
981 if (stream->fd >= 0)
982 closesocket(stream->fd);
983 stream->fd = -1;
984 streaming_ctrl_free(stream->streaming_ctrl);
985 stream->streaming_ctrl = NULL;
986 return STREAM_UNSUPPORTED;
989 return fixup_open(stream, seekable);
993 const stream_info_t stream_info_http1 = {
994 "http streaming",
995 "null",
996 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
997 "plain http",
998 open_s1,
999 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
1000 NULL,
1001 0 // Urls are an option string
1004 const stream_info_t stream_info_http2 = {
1005 "http streaming",
1006 "null",
1007 "Bertrand, Albeu, Arpi? who?",
1008 "plain http, also used as fallback for many other protocols",
1009 open_s2,
1010 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
1011 NULL,
1012 0 // Urls are an option string