Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / http.c
blobc1018ff4d3f4a5603ea4d423dea0376f337aa1b5
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"
45 extern const mime_struct_t mime_type_table[];
46 extern int stream_cache_size;
47 extern int network_bandwidth;
49 typedef struct {
50 unsigned metaint;
51 unsigned metapos;
52 int is_ultravox;
53 } scast_data_t;
55 /**
56 * \brief first read any data from sc->buffer then from fd
57 * \param fd file descriptor to read data from
58 * \param buffer buffer to read into
59 * \param len how many bytes to read
60 * \param sc streaming control containing buffer to read from first
61 * \return len unless there is a read error or eof
63 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
64 unsigned pos = 0;
65 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
66 if (cp_len > len)
67 cp_len = len;
68 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
69 sc->buffer_pos += cp_len;
70 pos += cp_len;
71 while (pos < len) {
72 int ret = recv(fd, &buffer[pos], len - pos, 0);
73 if (ret <= 0)
74 break;
75 pos += ret;
77 return pos;
80 /**
81 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
82 * \param fd file descriptor to read from
83 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
84 * \return number of real data before next metadata block starts or 0 on error
86 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
87 unsigned metaint;
88 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
89 int info_read;
90 do {
91 info_read = my_read(fd, info, 1, sc);
92 if (info[0] == 0x00)
93 info_read = my_read(fd, info, 6, sc);
94 else
95 info_read += my_read(fd, &info[1], 5, sc);
96 if (info_read != 6) // read error or eof
97 return 0;
98 // sync byte and reserved flags
99 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
100 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
101 return 0;
103 if (info[1] & 0x01)
104 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
105 metaint = info[4] << 8 | info[5];
106 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
107 char *metabuf = malloc(metaint);
108 my_read(fd, metabuf, metaint, sc);
109 free(metabuf);
111 } while ((info[3] & 0xf) < 0x07);
112 return metaint;
116 * \brief read one scast meta data entry and print it
117 * \param fd file descriptor to read from
118 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
120 static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
121 unsigned char tmp = 0;
122 unsigned metalen;
123 my_read(fd, &tmp, 1, sc);
124 metalen = tmp * 16;
125 if (metalen > 0) {
126 char *info = malloc(metalen + 1);
127 unsigned nlen = my_read(fd, info, metalen, sc);
128 info[nlen] = 0;
129 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
130 free(info);
135 * \brief read data from scast/ultravox stream without any metadata
136 * \param fd file descriptor to read from
137 * \param buffer buffer to read data into
138 * \param size number of bytes to read
139 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
141 static int scast_streaming_read(int fd, char *buffer, int size,
142 streaming_ctrl_t *sc) {
143 scast_data_t *sd = (scast_data_t *)sc->data;
144 unsigned block, ret;
145 unsigned done = 0;
147 // first read remaining data up to next metadata
148 block = sd->metaint - sd->metapos;
149 if (block > size)
150 block = size;
151 ret = my_read(fd, buffer, block, sc);
152 sd->metapos += ret;
153 done += ret;
154 if (ret != block) // read problems or eof
155 size = done;
157 while (done < size) { // now comes the metadata
158 if (sd->is_ultravox)
160 sd->metaint = uvox_meta_read(fd, sc);
161 if (!sd->metaint)
162 size = done;
164 else
165 scast_meta_read(fd, sc); // read and display metadata
166 sd->metapos = 0;
167 block = size - done;
168 if (block > sd->metaint)
169 block = sd->metaint;
170 ret = my_read(fd, &buffer[done], block, sc);
171 sd->metapos += ret;
172 done += ret;
173 if (ret != block) // read problems or eof
174 size = done;
176 return done;
179 static int scast_streaming_start(stream_t *stream) {
180 int metaint;
181 scast_data_t *scast_data;
182 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
183 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
184 if (!stream || stream->fd < 0 || !http_hdr)
185 return -1;
186 if (is_ultravox)
187 metaint = 0;
188 else {
189 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
190 if (metaint <= 0)
191 return -1;
193 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
194 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
195 stream->streaming_ctrl->buffer_pos = 0;
196 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
197 scast_data = malloc(sizeof(scast_data_t));
198 scast_data->metaint = metaint;
199 scast_data->metapos = 0;
200 scast_data->is_ultravox = is_ultravox;
201 http_free(http_hdr);
202 stream->streaming_ctrl->data = scast_data;
203 stream->streaming_ctrl->streaming_read = scast_streaming_read;
204 stream->streaming_ctrl->streaming_seek = NULL;
205 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
206 stream->streaming_ctrl->buffering = 1;
207 stream->streaming_ctrl->status = streaming_playing_e;
208 return 0;
211 static int nop_streaming_start( stream_t *stream ) {
212 HTTP_header_t *http_hdr = NULL;
213 char *next_url=NULL;
214 URL_t *rd_url=NULL;
215 int fd,ret;
216 if( stream==NULL ) return -1;
218 fd = stream->fd;
219 if( fd<0 ) {
220 fd = http_send_request( stream->streaming_ctrl->url, 0 );
221 if( fd<0 ) return -1;
222 http_hdr = http_read_response( fd );
223 if( http_hdr==NULL ) return -1;
225 switch( http_hdr->status_code ) {
226 case 200: // OK
227 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
228 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
229 if( http_hdr->body_size>0 ) {
230 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
231 http_free( http_hdr );
232 return -1;
235 break;
236 // Redirect
237 case 301: // Permanently
238 case 302: // Temporarily
239 case 303: // See Other
240 ret=-1;
241 next_url = http_get_field( http_hdr, "Location" );
243 if (next_url != NULL)
244 rd_url=url_new(next_url);
246 if (next_url != NULL && rd_url != NULL) {
247 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
248 stream->streaming_ctrl->url=check4proxies(rd_url);
249 ret=nop_streaming_start(stream); //recursively get streaming started
250 } else {
251 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
252 closesocket( fd );
253 fd = -1;
255 return ret;
256 break;
257 case 401: //Authorization required
258 case 403: //Forbidden
259 case 404: //Not found
260 case 500: //Server Error
261 default:
262 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
263 closesocket( fd );
264 fd = -1;
265 return -1;
266 break;
268 stream->fd = fd;
269 } else {
270 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
271 if( http_hdr->body_size>0 ) {
272 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
273 http_free( http_hdr );
274 stream->streaming_ctrl->data = NULL;
275 return -1;
280 if( http_hdr ) {
281 http_free( http_hdr );
282 stream->streaming_ctrl->data = NULL;
285 stream->streaming_ctrl->streaming_read = nop_streaming_read;
286 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
287 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
288 stream->streaming_ctrl->buffering = 1;
289 stream->streaming_ctrl->status = streaming_playing_e;
290 return 0;
293 HTTP_header_t *
294 http_new_header(void) {
295 HTTP_header_t *http_hdr;
297 http_hdr = malloc(sizeof(HTTP_header_t));
298 if( http_hdr==NULL ) return NULL;
299 memset( http_hdr, 0, sizeof(HTTP_header_t) );
301 return http_hdr;
304 void
305 http_free( HTTP_header_t *http_hdr ) {
306 HTTP_field_t *field, *field2free;
307 if( http_hdr==NULL ) return;
308 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
309 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
310 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
311 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
312 if( http_hdr->method!=NULL ) free( http_hdr->method );
313 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
314 field = http_hdr->first_field;
315 while( field!=NULL ) {
316 field2free = field;
317 if (field->field_name)
318 free(field->field_name);
319 field = field->next;
320 free( field2free );
322 free( http_hdr );
323 http_hdr = NULL;
327 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
328 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
330 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
331 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
332 return -1;
334 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
335 if( http_hdr->buffer==NULL ) {
336 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
337 return -1;
339 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
340 http_hdr->buffer_size += length;
341 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
342 return http_hdr->buffer_size;
346 http_is_header_entire( HTTP_header_t *http_hdr ) {
347 if( http_hdr==NULL ) return -1;
348 if( http_hdr->buffer==NULL ) return 0; // empty
350 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
351 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
352 return 1;
356 http_response_parse( HTTP_header_t *http_hdr ) {
357 char *hdr_ptr, *ptr;
358 char *field=NULL;
359 int pos_hdr_sep, hdr_sep_len;
360 size_t len;
361 if( http_hdr==NULL ) return -1;
362 if( http_hdr->is_parsed ) return 0;
364 // Get the protocol
365 hdr_ptr = strstr( http_hdr->buffer, " " );
366 if( hdr_ptr==NULL ) {
367 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
368 return -1;
370 len = hdr_ptr-http_hdr->buffer;
371 http_hdr->protocol = malloc(len+1);
372 if( http_hdr->protocol==NULL ) {
373 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
374 return -1;
376 strncpy( http_hdr->protocol, http_hdr->buffer, len );
377 http_hdr->protocol[len]='\0';
378 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
379 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
380 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
381 return -1;
385 // Get the status code
386 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
387 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
388 return -1;
390 hdr_ptr += 4;
392 // Get the reason phrase
393 ptr = strstr( hdr_ptr, "\n" );
394 if( hdr_ptr==NULL ) {
395 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
396 return -1;
398 len = ptr-hdr_ptr;
399 http_hdr->reason_phrase = malloc(len+1);
400 if( http_hdr->reason_phrase==NULL ) {
401 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
402 return -1;
404 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
405 if( http_hdr->reason_phrase[len-1]=='\r' ) {
406 len--;
408 http_hdr->reason_phrase[len]='\0';
410 // Set the position of the header separator: \r\n\r\n
411 hdr_sep_len = 4;
412 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
413 if( ptr==NULL ) {
414 ptr = strstr( http_hdr->buffer, "\n\n" );
415 if( ptr==NULL ) {
416 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
417 return -1;
419 hdr_sep_len = 2;
421 pos_hdr_sep = ptr-http_hdr->buffer;
423 // Point to the first line after the method line.
424 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
425 do {
426 ptr = hdr_ptr;
427 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
428 len = ptr-hdr_ptr;
429 if( len==0 ) break;
430 field = realloc(field, len+1);
431 if( field==NULL ) {
432 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
433 return -1;
435 strncpy( field, hdr_ptr, len );
436 field[len]='\0';
437 http_set_field( http_hdr, field );
438 hdr_ptr = ptr+((*ptr=='\r')?2:1);
439 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
441 if( field!=NULL ) free( field );
443 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
444 // Response has data!
445 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
446 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
449 http_hdr->is_parsed = 1;
450 return 0;
453 char *
454 http_build_request( HTTP_header_t *http_hdr ) {
455 char *ptr, *uri=NULL;
456 int len;
457 HTTP_field_t *field;
458 if( http_hdr==NULL ) return NULL;
460 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
461 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
462 else {
463 uri = malloc(strlen(http_hdr->uri) + 1);
464 if( uri==NULL ) {
465 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
466 return NULL;
468 strcpy(uri,http_hdr->uri);
471 //**** Compute the request length
472 // Add the Method line
473 len = strlen(http_hdr->method)+strlen(uri)+12;
474 // Add the fields
475 field = http_hdr->first_field;
476 while( field!=NULL ) {
477 len += strlen(field->field_name)+2;
478 field = field->next;
480 // Add the CRLF
481 len += 2;
482 // Add the body
483 if( http_hdr->body!=NULL ) {
484 len += http_hdr->body_size;
486 // Free the buffer if it was previously used
487 if( http_hdr->buffer!=NULL ) {
488 free( http_hdr->buffer );
489 http_hdr->buffer = NULL;
491 http_hdr->buffer = malloc(len+1);
492 if( http_hdr->buffer==NULL ) {
493 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
494 return NULL;
496 http_hdr->buffer_size = len;
498 //*** Building the request
499 ptr = http_hdr->buffer;
500 // Add the method line
501 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
502 field = http_hdr->first_field;
503 // Add the field
504 while( field!=NULL ) {
505 ptr += sprintf( ptr, "%s\r\n", field->field_name );
506 field = field->next;
508 ptr += sprintf( ptr, "\r\n" );
509 // Add the body
510 if( http_hdr->body!=NULL ) {
511 memcpy( ptr, http_hdr->body, http_hdr->body_size );
514 if( uri ) free( uri );
515 return http_hdr->buffer;
518 char *
519 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
520 if( http_hdr==NULL || field_name==NULL ) return NULL;
521 http_hdr->field_search_pos = http_hdr->first_field;
522 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 );
523 if( http_hdr->field_search==NULL ) {
524 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
525 return NULL;
527 strcpy( http_hdr->field_search, field_name );
528 return http_get_next_field( http_hdr );
531 char *
532 http_get_next_field( HTTP_header_t *http_hdr ) {
533 char *ptr;
534 HTTP_field_t *field;
535 if( http_hdr==NULL ) return NULL;
537 field = http_hdr->field_search_pos;
538 while( field!=NULL ) {
539 ptr = strstr( field->field_name, ":" );
540 if( ptr==NULL ) return NULL;
541 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
542 ptr++; // Skip the column
543 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
544 http_hdr->field_search_pos = field->next;
545 return ptr; // return the value without the field name
547 field = field->next;
549 return NULL;
552 void
553 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
554 HTTP_field_t *new_field;
555 if( http_hdr==NULL || field_name==NULL ) return;
557 new_field = malloc(sizeof(HTTP_field_t));
558 if( new_field==NULL ) {
559 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
560 return;
562 new_field->next = NULL;
563 new_field->field_name = malloc(strlen(field_name)+1);
564 if( new_field->field_name==NULL ) {
565 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
566 free(new_field);
567 return;
569 strcpy( new_field->field_name, field_name );
571 if( http_hdr->last_field==NULL ) {
572 http_hdr->first_field = new_field;
573 } else {
574 http_hdr->last_field->next = new_field;
576 http_hdr->last_field = new_field;
577 http_hdr->field_nb++;
580 void
581 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
582 if( http_hdr==NULL || method==NULL ) return;
584 http_hdr->method = malloc(strlen(method)+1);
585 if( http_hdr->method==NULL ) {
586 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
587 return;
589 strcpy( http_hdr->method, method );
592 void
593 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
594 if( http_hdr==NULL || uri==NULL ) return;
596 http_hdr->uri = malloc(strlen(uri)+1);
597 if( http_hdr->uri==NULL ) {
598 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
599 return;
601 strcpy( http_hdr->uri, uri );
605 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
606 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
607 int encoded_len, pass_len=0, out_len;
608 int res = -1;
609 if( http_hdr==NULL || username==NULL ) return -1;
611 if( password!=NULL ) {
612 pass_len = strlen(password);
615 usr_pass = malloc(strlen(username)+pass_len+2);
616 if( usr_pass==NULL ) {
617 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
618 goto out;
621 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
623 // Base 64 encode with at least 33% more data than the original size
624 encoded_len = strlen(usr_pass)*2;
625 b64_usr_pass = malloc(encoded_len);
626 if( b64_usr_pass==NULL ) {
627 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
628 goto out;
631 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
632 if( out_len<0 ) {
633 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
634 goto out;
637 b64_usr_pass[out_len]='\0';
639 auth = malloc(encoded_len+22);
640 if( auth==NULL ) {
641 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
642 goto out;
645 sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
646 http_set_field( http_hdr, auth );
647 res = 0;
649 out:
650 free( usr_pass );
651 free( b64_usr_pass );
652 free( auth );
654 return res;
657 void
658 http_debug_hdr( HTTP_header_t *http_hdr ) {
659 HTTP_field_t *field;
660 int i = 0;
661 if( http_hdr==NULL ) return;
663 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
664 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
665 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
666 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
667 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
668 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
669 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
670 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size );
672 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
673 field = http_hdr->first_field;
674 while( field!=NULL ) {
675 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
676 field = field->next;
678 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
682 base64_encode(const void *enc, int encLen, char *out, int outMax) {
683 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
685 unsigned char *encBuf;
686 int outLen;
687 unsigned int bits;
688 unsigned int shift;
690 encBuf = (unsigned char*)enc;
691 outLen = 0;
692 bits = 0;
693 shift = 0;
694 outMax &= ~3;
696 while(1) {
697 if( encLen>0 ) {
698 // Shift in byte
699 bits <<= 8;
700 bits |= *encBuf;
701 shift += 8;
702 // Next byte
703 encBuf++;
704 encLen--;
705 } else if( shift>0 ) {
706 // Pad last bits to 6 bits - will end next loop
707 bits <<= 6 - shift;
708 shift = 6;
709 } else {
710 // As per RFC 2045, section 6.8,
711 // pad output as necessary: 0 to 2 '=' chars.
712 while( outLen & 3 ){
713 *out++ = '=';
714 outLen++;
717 return outLen;
720 // Encode 6 bit segments
721 while( shift>=6 ) {
722 if (outLen >= outMax)
723 return -1;
724 shift -= 6;
725 *out = b64[ (bits >> shift) & 0x3F ];
726 out++;
727 outLen++;
732 static void print_icy_metadata(HTTP_header_t *http_hdr) {
733 const char *field_data;
734 // note: I skip icy-notice1 and 2, as they contain html <BR>
735 // and are IMHO useless info ::atmos
736 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
737 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
738 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
739 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
740 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
741 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
742 // XXX: does this really mean public server? ::atmos
743 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
744 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
745 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
746 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
749 //! If this function succeeds you must closesocket stream->fd
750 static int http_streaming_start(stream_t *stream, int* file_format) {
751 HTTP_header_t *http_hdr = NULL;
752 unsigned int i;
753 int fd = stream->fd;
754 int res = STREAM_UNSUPPORTED;
755 int redirect = 0;
756 int auth_retry=0;
757 int seekable=0;
758 char *content_type;
759 const char *content_length;
760 char *next_url;
761 URL_t *url = stream->streaming_ctrl->url;
765 redirect = 0;
766 if (fd > 0) closesocket(fd);
767 fd = http_send_request( url, 0 );
768 if( fd<0 ) {
769 goto err_out;
772 http_free(http_hdr);
773 http_hdr = http_read_response( fd );
774 if( http_hdr==NULL ) {
775 goto err_out;
778 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
779 http_debug_hdr( http_hdr );
782 // Check if we can make partial content requests and thus seek in http-streams
783 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
784 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
785 const char *server = http_get_field(http_hdr, "Server");
786 if (accept_ranges)
787 seekable = strncmp(accept_ranges,"bytes",5)==0;
788 else if (server && strcmp(server, "gvs 1.0") == 0)
789 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
792 print_icy_metadata(http_hdr);
794 // Check if the response is an ICY status_code reason_phrase
795 if( !strcasecmp(http_hdr->protocol, "ICY") ||
796 http_get_field(http_hdr, "Icy-MetaInt") ) {
797 switch( http_hdr->status_code ) {
798 case 200: { // OK
799 char *field_data;
800 // If content-type == video/nsv we most likely have a winamp video stream
801 // otherwise it should be mp3. if there are more types consider adding mime type
802 // handling like later
803 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
804 *file_format = DEMUXER_TYPE_NSV;
805 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
806 *file_format = DEMUXER_TYPE_AAC;
807 else
808 *file_format = DEMUXER_TYPE_AUDIO;
809 res = STREAM_ERROR;
810 goto out;
812 case 400: // Server Full
813 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
814 goto err_out;
815 case 401: // Service Unavailable
816 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
817 goto err_out;
818 case 403: // Service Forbidden
819 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
820 goto err_out;
821 case 404: // Resource Not Found
822 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
823 goto err_out;
824 default:
825 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
826 goto err_out;
830 // Assume standard http if not ICY
831 switch( http_hdr->status_code ) {
832 case 200: // OK
833 content_length = http_get_field(http_hdr, "Content-Length");
834 if (content_length) {
835 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
836 stream->end_pos = atoll(content_length);
838 // Look if we can use the Content-Type
839 content_type = http_get_field( http_hdr, "Content-Type" );
840 if( content_type!=NULL ) {
841 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
842 // Check in the mime type table for a demuxer type
843 i = 0;
844 while(mime_type_table[i].mime_type != NULL) {
845 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
846 *file_format = mime_type_table[i].demuxer_type;
847 res = seekable;
848 goto out;
850 i++;
853 // Not found in the mime type table, don't fail,
854 // we should try raw HTTP
855 res = seekable;
856 goto out;
857 // Redirect
858 case 301: // Permanently
859 case 302: // Temporarily
860 case 303: // See Other
861 // TODO: RFC 2616, recommand to detect infinite redirection loops
862 next_url = http_get_field( http_hdr, "Location" );
863 if( next_url!=NULL ) {
864 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
865 stream->streaming_ctrl->url = url_redirect( &url, next_url );
866 if (!strcasecmp(url->protocol, "mms")) {
867 res = STREAM_REDIRECTED;
868 goto err_out;
870 if (strcasecmp(url->protocol, "http")) {
871 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
872 goto err_out;
874 if (is_ultravox) {
875 free(url->protocol);
876 url->protocol = strdup("unsv");
878 redirect = 1;
880 break;
881 case 401: // Authentication required
882 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
883 goto err_out;
884 redirect = 1;
885 break;
886 default:
887 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
888 goto err_out;
890 } while( redirect );
892 err_out:
893 if (fd > 0) closesocket( fd );
894 fd = -1;
895 http_free( http_hdr );
896 http_hdr = NULL;
897 out:
898 stream->streaming_ctrl->data = (void*)http_hdr;
899 stream->fd = fd;
900 return res;
903 static int fixup_open(stream_t *stream,int seekable) {
904 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
905 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
906 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
908 stream->type = STREAMTYPE_STREAM;
909 if(!is_icy && !is_ultravox && seekable)
911 stream->flags |= MP_STREAM_SEEK;
912 stream->seek = http_seek;
914 stream->streaming_ctrl->bandwidth = network_bandwidth;
915 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
916 if(nop_streaming_start( stream )) {
917 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
918 if (stream->fd >= 0)
919 closesocket(stream->fd);
920 stream->fd = -1;
921 streaming_ctrl_free(stream->streaming_ctrl);
922 stream->streaming_ctrl = NULL;
923 return STREAM_UNSUPPORTED;
926 fixup_network_stream_cache(stream);
927 return STREAM_OK;
930 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
931 int seekable=0;
932 URL_t *url;
934 stream->streaming_ctrl = streaming_ctrl_new();
935 if( stream->streaming_ctrl==NULL ) {
936 return STREAM_ERROR;
938 stream->streaming_ctrl->bandwidth = network_bandwidth;
939 url = url_new(stream->url);
940 stream->streaming_ctrl->url = check4proxies(url);
941 url_free(url);
943 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
944 seekable = http_streaming_start(stream, file_format);
945 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
946 if (stream->fd >= 0)
947 closesocket(stream->fd);
948 stream->fd = -1;
949 if (seekable == STREAM_REDIRECTED)
950 return seekable;
951 streaming_ctrl_free(stream->streaming_ctrl);
952 stream->streaming_ctrl = NULL;
953 return STREAM_UNSUPPORTED;
956 return fixup_open(stream, seekable);
959 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
960 int seekable=0;
961 URL_t *url;
963 stream->streaming_ctrl = streaming_ctrl_new();
964 if( stream->streaming_ctrl==NULL ) {
965 return STREAM_ERROR;
967 stream->streaming_ctrl->bandwidth = network_bandwidth;
968 url = url_new(stream->url);
969 stream->streaming_ctrl->url = check4proxies(url);
970 url_free(url);
972 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
973 seekable = http_streaming_start(stream, file_format);
974 if(seekable < 0) {
975 if (stream->fd >= 0)
976 closesocket(stream->fd);
977 stream->fd = -1;
978 streaming_ctrl_free(stream->streaming_ctrl);
979 stream->streaming_ctrl = NULL;
980 return STREAM_UNSUPPORTED;
983 return fixup_open(stream, seekable);
987 const stream_info_t stream_info_http1 = {
988 "http streaming",
989 "null",
990 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
991 "plain http",
992 open_s1,
993 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
994 NULL,
995 0 // Urls are an option string
998 const stream_info_t stream_info_http2 = {
999 "http streaming",
1000 "null",
1001 "Bertrand, Albeu, Arpi? who?",
1002 "plain http, also used as fallback for many other protocols",
1003 open_s2,
1004 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
1005 NULL,
1006 0 // Urls are an option string