Mercurial > mplayer.hg
annotate stream/http.c @ 31524:083786e4aaf2
MP3 decoding through libmpg123
patch by Thomas Orgis, thomas-forum orgis org
author | diego |
---|---|
date | Wed, 30 Jun 2010 09:55:14 +0000 |
parents | 9494acd724a9 |
children | c680009d6779 |
rev | line source |
---|---|
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
1 /* |
902 | 2 * HTTP Helper |
30426
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
3 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com> |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
5 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
6 * This file is part of MPlayer. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
7 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
8 * MPlayer is free software; you can redistribute it and/or modify |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
11 * (at your option) any later version. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
12 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
13 * MPlayer is distributed in the hope that it will be useful, |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
16 * GNU General Public License for more details. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
17 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
18 * You should have received a copy of the GNU General Public License along |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
30357
diff
changeset
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
902 | 21 */ |
22 | |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
23 #include "config.h" |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
24 |
870 | 25 #include <stdio.h> |
26 #include <stdlib.h> | |
27 #include <string.h> | |
15585 | 28 #include <unistd.h> |
870 | 29 |
28402 | 30 #if !HAVE_WINSOCK2_H |
27472
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
31 #else |
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
32 #include <winsock2.h> |
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
33 #include <ws2tcpip.h> |
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
34 #endif |
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
35 |
c0b233cd30ca
Revert moving closesocket definition and network headers to network.h.
diego
parents:
27464
diff
changeset
|
36 #include "http.h" |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
37 #include "url.h" |
5915 | 38 #include "mp_msg.h" |
870 | 39 |
15585 | 40 #include "stream.h" |
19312
ab8d6b6deb63
proper inclusion of demuxer.h (including libmpdemux in Makefile only was to make previous split easier)
ben
parents:
19271
diff
changeset
|
41 #include "libmpdemux/demuxer.h" |
15585 | 42 #include "network.h" |
43 #include "help_mp.h" | |
44 | |
45 | |
25246 | 46 extern const mime_struct_t mime_type_table[]; |
15585 | 47 extern int stream_cache_size; |
48 | |
16013 | 49 typedef struct { |
50 unsigned metaint; | |
51 unsigned metapos; | |
52 int is_ultravox; | |
53 } scast_data_t; | |
54 | |
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 | |
62 */ | |
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) { | |
16070 | 72 int ret = recv(fd, &buffer[pos], len - pos, 0); |
16013 | 73 if (ret <= 0) |
74 break; | |
75 pos += ret; | |
76 } | |
77 return pos; | |
78 } | |
79 | |
16032 | 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 | |
85 */ | |
16013 | 86 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { |
87 unsigned metaint; | |
16070 | 88 unsigned char info[6] = {0, 0, 0, 0, 0, 0}; |
89 int info_read; | |
16013 | 90 do { |
16070 | 91 info_read = my_read(fd, info, 1, sc); |
16013 | 92 if (info[0] == 0x00) |
16070 | 93 info_read = my_read(fd, info, 6, sc); |
16013 | 94 else |
16070 | 95 info_read += my_read(fd, &info[1], 5, sc); |
96 if (info_read != 6) // read error or eof | |
97 return 0; | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
98 // sync byte and reserved flags |
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
99 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { |
16013 | 100 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); |
101 return 0; | |
102 } | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
103 if (info[1] & 0x01) |
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
104 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); |
16013 | 105 metaint = info[4] << 8 | info[5]; |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
106 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense |
16013 | 107 char *metabuf = malloc(metaint); |
108 my_read(fd, metabuf, metaint, sc); | |
109 free(metabuf); | |
110 } | |
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
111 } while ((info[3] & 0xf) < 0x07); |
16013 | 112 return metaint; |
113 } | |
114 | |
115 /** | |
116 * \brief read one scast meta data entry and print it | |
16032 | 117 * \param fd file descriptor to read from |
118 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
16013 | 119 */ |
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) { | |
30938 | 126 int i; |
30941
fb3bde3ec3a8
Change type to uint8_t to avoid checks depending on char signedness.
reimar
parents:
30938
diff
changeset
|
127 uint8_t *info = malloc(metalen + 1); |
16013 | 128 unsigned nlen = my_read(fd, info, metalen, sc); |
30938 | 129 // avoid breaking the user's terminal too much |
130 if (nlen > 256) nlen = 256; | |
131 for (i = 0; i < nlen; i++) | |
132 if (info[i] && info[i] < 32) info[i] = '?'; | |
16013 | 133 info[nlen] = 0; |
134 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); | |
135 free(info); | |
136 } | |
137 } | |
138 | |
16032 | 139 /** |
140 * \brief read data from scast/ultravox stream without any metadata | |
141 * \param fd file descriptor to read from | |
142 * \param buffer buffer to read data into | |
143 * \param size number of bytes to read | |
144 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
145 */ | |
16013 | 146 static int scast_streaming_read(int fd, char *buffer, int size, |
147 streaming_ctrl_t *sc) { | |
148 scast_data_t *sd = (scast_data_t *)sc->data; | |
149 unsigned block, ret; | |
150 unsigned done = 0; | |
151 | |
152 // first read remaining data up to next metadata | |
153 block = sd->metaint - sd->metapos; | |
154 if (block > size) | |
155 block = size; | |
156 ret = my_read(fd, buffer, block, sc); | |
157 sd->metapos += ret; | |
158 done += ret; | |
159 if (ret != block) // read problems or eof | |
160 size = done; | |
161 | |
162 while (done < size) { // now comes the metadata | |
163 if (sd->is_ultravox) | |
16070 | 164 { |
16013 | 165 sd->metaint = uvox_meta_read(fd, sc); |
16070 | 166 if (!sd->metaint) |
167 size = done; | |
168 } | |
16013 | 169 else |
170 scast_meta_read(fd, sc); // read and display metadata | |
171 sd->metapos = 0; | |
172 block = size - done; | |
173 if (block > sd->metaint) | |
174 block = sd->metaint; | |
175 ret = my_read(fd, &buffer[done], block, sc); | |
176 sd->metapos += ret; | |
177 done += ret; | |
178 if (ret != block) // read problems or eof | |
179 size = done; | |
180 } | |
181 return done; | |
182 } | |
183 | |
184 static int scast_streaming_start(stream_t *stream) { | |
185 int metaint; | |
186 scast_data_t *scast_data; | |
187 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
16070 | 188 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
16013 | 189 if (!stream || stream->fd < 0 || !http_hdr) |
190 return -1; | |
191 if (is_ultravox) | |
192 metaint = 0; | |
193 else { | |
194 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt")); | |
195 if (metaint <= 0) | |
196 return -1; | |
197 } | |
198 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); | |
199 stream->streaming_ctrl->buffer_size = http_hdr->body_size; | |
200 stream->streaming_ctrl->buffer_pos = 0; | |
201 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); | |
202 scast_data = malloc(sizeof(scast_data_t)); | |
203 scast_data->metaint = metaint; | |
204 scast_data->metapos = 0; | |
205 scast_data->is_ultravox = is_ultravox; | |
206 http_free(http_hdr); | |
207 stream->streaming_ctrl->data = scast_data; | |
208 stream->streaming_ctrl->streaming_read = scast_streaming_read; | |
209 stream->streaming_ctrl->streaming_seek = NULL; | |
210 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes | |
211 stream->streaming_ctrl->buffering = 1; | |
212 stream->streaming_ctrl->status = streaming_playing_e; | |
213 return 0; | |
214 } | |
215 | |
15585 | 216 static int nop_streaming_start( stream_t *stream ) { |
217 HTTP_header_t *http_hdr = NULL; | |
218 char *next_url=NULL; | |
219 URL_t *rd_url=NULL; | |
220 int fd,ret; | |
221 if( stream==NULL ) return -1; | |
222 | |
223 fd = stream->fd; | |
224 if( fd<0 ) { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
225 fd = http_send_request( stream->streaming_ctrl->url, 0 ); |
15585 | 226 if( fd<0 ) return -1; |
227 http_hdr = http_read_response( fd ); | |
228 if( http_hdr==NULL ) return -1; | |
229 | |
230 switch( http_hdr->status_code ) { | |
231 case 200: // OK | |
232 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
233 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
234 if( http_hdr->body_size>0 ) { | |
235 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
236 http_free( http_hdr ); | |
237 return -1; | |
238 } | |
239 } | |
240 break; | |
241 // Redirect | |
242 case 301: // Permanently | |
243 case 302: // Temporarily | |
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
244 case 303: // See Other |
15585 | 245 ret=-1; |
246 next_url = http_get_field( http_hdr, "Location" ); | |
247 | |
248 if (next_url != NULL) | |
249 rd_url=url_new(next_url); | |
250 | |
251 if (next_url != NULL && rd_url != NULL) { | |
252 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); | |
253 stream->streaming_ctrl->url=check4proxies(rd_url); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
254 ret=nop_streaming_start(stream); //recursively get streaming started |
15585 | 255 } else { |
256 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
257 closesocket( fd ); | |
258 fd = -1; | |
259 } | |
260 return ret; | |
261 break; | |
262 case 401: //Authorization required | |
263 case 403: //Forbidden | |
264 case 404: //Not found | |
265 case 500: //Server Error | |
266 default: | |
267 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
268 closesocket( fd ); | |
269 fd = -1; | |
270 return -1; | |
271 break; | |
272 } | |
273 stream->fd = fd; | |
274 } else { | |
275 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
276 if( http_hdr->body_size>0 ) { | |
277 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
278 http_free( http_hdr ); | |
279 stream->streaming_ctrl->data = NULL; | |
280 return -1; | |
281 } | |
282 } | |
283 } | |
284 | |
285 if( http_hdr ) { | |
286 http_free( http_hdr ); | |
287 stream->streaming_ctrl->data = NULL; | |
288 } | |
289 | |
290 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
291 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
292 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
293 stream->streaming_ctrl->buffering = 1; | |
294 stream->streaming_ctrl->status = streaming_playing_e; | |
295 return 0; | |
296 } | |
297 | |
870 | 298 HTTP_header_t * |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16948
diff
changeset
|
299 http_new_header(void) { |
870 | 300 HTTP_header_t *http_hdr; |
301 | |
18879 | 302 http_hdr = malloc(sizeof(HTTP_header_t)); |
870 | 303 if( http_hdr==NULL ) return NULL; |
304 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
305 | |
306 return http_hdr; | |
307 } | |
308 | |
309 void | |
310 http_free( HTTP_header_t *http_hdr ) { | |
3039 | 311 HTTP_field_t *field, *field2free; |
870 | 312 if( http_hdr==NULL ) return; |
313 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
314 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
315 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
316 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
902 | 317 if( http_hdr->method!=NULL ) free( http_hdr->method ); |
318 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
3039 | 319 field = http_hdr->first_field; |
320 while( field!=NULL ) { | |
321 field2free = field; | |
14460 | 322 if (field->field_name) |
323 free(field->field_name); | |
3039 | 324 field = field->next; |
325 free( field2free ); | |
326 } | |
870 | 327 free( http_hdr ); |
3039 | 328 http_hdr = NULL; |
870 | 329 } |
330 | |
902 | 331 int |
332 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
1027 | 333 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
334 |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
335 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) { |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
336 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n"); |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
337 return -1; |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
338 } |
30702 | 339 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); |
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
340 if( http_hdr->buffer==NULL ) { |
7293 | 341 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); |
902 | 342 return -1; |
343 } | |
7293 | 344 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
345 http_hdr->buffer_size += length; | |
346 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
902 | 347 return http_hdr->buffer_size; |
348 } | |
349 | |
350 int | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
351 http_is_header_entire( HTTP_header_t *http_hdr ) { |
902 | 352 if( http_hdr==NULL ) return -1; |
7293 | 353 if( http_hdr->buffer==NULL ) return 0; // empty |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
354 |
3784 | 355 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
356 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
357 return 1; | |
902 | 358 } |
359 | |
360 int | |
361 http_response_parse( HTTP_header_t *http_hdr ) { | |
870 | 362 char *hdr_ptr, *ptr; |
363 char *field=NULL; | |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
364 int pos_hdr_sep, hdr_sep_len; |
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
365 size_t len; |
902 | 366 if( http_hdr==NULL ) return -1; |
367 if( http_hdr->is_parsed ) return 0; | |
870 | 368 |
369 // Get the protocol | |
902 | 370 hdr_ptr = strstr( http_hdr->buffer, " " ); |
870 | 371 if( hdr_ptr==NULL ) { |
5915 | 372 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
902 | 373 return -1; |
870 | 374 } |
902 | 375 len = hdr_ptr-http_hdr->buffer; |
18879 | 376 http_hdr->protocol = malloc(len+1); |
870 | 377 if( http_hdr->protocol==NULL ) { |
5915 | 378 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 379 return -1; |
870 | 380 } |
902 | 381 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
382 http_hdr->protocol[len]='\0'; | |
870 | 383 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
384 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
5915 | 385 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
902 | 386 return -1; |
870 | 387 } |
388 } | |
389 | |
390 // Get the status code | |
391 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
5915 | 392 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
902 | 393 return -1; |
870 | 394 } |
395 hdr_ptr += 4; | |
396 | |
397 // Get the reason phrase | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
398 ptr = strstr( hdr_ptr, "\n" ); |
870 | 399 if( hdr_ptr==NULL ) { |
5915 | 400 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
902 | 401 return -1; |
870 | 402 } |
403 len = ptr-hdr_ptr; | |
18879 | 404 http_hdr->reason_phrase = malloc(len+1); |
870 | 405 if( http_hdr->reason_phrase==NULL ) { |
5915 | 406 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
902 | 407 return -1; |
870 | 408 } |
409 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
4311 | 410 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
411 len--; | |
412 } | |
870 | 413 http_hdr->reason_phrase[len]='\0'; |
414 | |
415 // Set the position of the header separator: \r\n\r\n | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
416 hdr_sep_len = 4; |
902 | 417 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
870 | 418 if( ptr==NULL ) { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
419 ptr = strstr( http_hdr->buffer, "\n\n" ); |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
420 if( ptr==NULL ) { |
5915 | 421 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n"); |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
422 return -1; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
423 } |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
424 hdr_sep_len = 2; |
870 | 425 } |
902 | 426 pos_hdr_sep = ptr-http_hdr->buffer; |
870 | 427 |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
428 // Point to the first line after the method line. |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
429 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
870 | 430 do { |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
431 ptr = hdr_ptr; |
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
432 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
870 | 433 len = ptr-hdr_ptr; |
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
434 if( len==0 ) break; |
30702 | 435 field = realloc(field, len+1); |
870 | 436 if( field==NULL ) { |
5915 | 437 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 438 return -1; |
870 | 439 } |
440 strncpy( field, hdr_ptr, len ); | |
441 field[len]='\0'; | |
442 http_set_field( http_hdr, field ); | |
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
443 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
902 | 444 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
445 |
870 | 446 if( field!=NULL ) free( field ); |
447 | |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
448 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { |
870 | 449 // Response has data! |
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
450 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; |
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
451 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); |
870 | 452 } |
453 | |
902 | 454 http_hdr->is_parsed = 1; |
455 return 0; | |
870 | 456 } |
457 | |
458 char * | |
902 | 459 http_build_request( HTTP_header_t *http_hdr ) { |
3497 | 460 char *ptr, *uri=NULL; |
902 | 461 int len; |
3039 | 462 HTTP_field_t *field; |
870 | 463 if( http_hdr==NULL ) return NULL; |
464 | |
465 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
466 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
3497 | 467 else { |
18879 | 468 uri = malloc(strlen(http_hdr->uri) + 1); |
3497 | 469 if( uri==NULL ) { |
5915 | 470 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
3497 | 471 return NULL; |
472 } | |
12391 | 473 strcpy(uri,http_hdr->uri); |
3497 | 474 } |
870 | 475 |
3497 | 476 //**** Compute the request length |
477 // Add the Method line | |
478 len = strlen(http_hdr->method)+strlen(uri)+12; | |
479 // Add the fields | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
480 field = http_hdr->first_field; |
3039 | 481 while( field!=NULL ) { |
482 len += strlen(field->field_name)+2; | |
483 field = field->next; | |
484 } | |
3497 | 485 // Add the CRLF |
486 len += 2; | |
487 // Add the body | |
902 | 488 if( http_hdr->body!=NULL ) { |
489 len += http_hdr->body_size; | |
490 } | |
3497 | 491 // Free the buffer if it was previously used |
902 | 492 if( http_hdr->buffer!=NULL ) { |
493 free( http_hdr->buffer ); | |
494 http_hdr->buffer = NULL; | |
495 } | |
18879 | 496 http_hdr->buffer = malloc(len+1); |
902 | 497 if( http_hdr->buffer==NULL ) { |
5915 | 498 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
902 | 499 return NULL; |
500 } | |
501 http_hdr->buffer_size = len; | |
502 | |
3497 | 503 //*** Building the request |
902 | 504 ptr = http_hdr->buffer; |
3497 | 505 // Add the method line |
506 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
3039 | 507 field = http_hdr->first_field; |
3497 | 508 // Add the field |
3039 | 509 while( field!=NULL ) { |
510 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
511 field = field->next; | |
512 } | |
870 | 513 ptr += sprintf( ptr, "\r\n" ); |
3497 | 514 // Add the body |
870 | 515 if( http_hdr->body!=NULL ) { |
516 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
517 } | |
3497 | 518 |
519 if( uri ) free( uri ); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
520 return http_hdr->buffer; |
870 | 521 } |
522 | |
523 char * | |
524 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
525 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
3039 | 526 http_hdr->field_search_pos = http_hdr->first_field; |
30702 | 527 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 ); |
870 | 528 if( http_hdr->field_search==NULL ) { |
5915 | 529 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 530 return NULL; |
531 } | |
532 strcpy( http_hdr->field_search, field_name ); | |
533 return http_get_next_field( http_hdr ); | |
534 } | |
535 | |
536 char * | |
537 http_get_next_field( HTTP_header_t *http_hdr ) { | |
538 char *ptr; | |
3039 | 539 HTTP_field_t *field; |
870 | 540 if( http_hdr==NULL ) return NULL; |
541 | |
3039 | 542 field = http_hdr->field_search_pos; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
543 while( field!=NULL ) { |
3039 | 544 ptr = strstr( field->field_name, ":" ); |
870 | 545 if( ptr==NULL ) return NULL; |
3039 | 546 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
870 | 547 ptr++; // Skip the column |
548 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
3039 | 549 http_hdr->field_search_pos = field->next; |
870 | 550 return ptr; // return the value without the field name |
551 } | |
3039 | 552 field = field->next; |
870 | 553 } |
554 return NULL; | |
555 } | |
556 | |
557 void | |
3039 | 558 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
559 HTTP_field_t *new_field; | |
560 if( http_hdr==NULL || field_name==NULL ) return; | |
870 | 561 |
18879 | 562 new_field = malloc(sizeof(HTTP_field_t)); |
3039 | 563 if( new_field==NULL ) { |
5915 | 564 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 565 return; |
566 } | |
3039 | 567 new_field->next = NULL; |
18879 | 568 new_field->field_name = malloc(strlen(field_name)+1); |
3039 | 569 if( new_field->field_name==NULL ) { |
5915 | 570 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
27834
d35bcab9833b
Avoid a memleak if allocation of field_name fails, fixes bug #1319.
reimar
parents:
27473
diff
changeset
|
571 free(new_field); |
3039 | 572 return; |
573 } | |
574 strcpy( new_field->field_name, field_name ); | |
575 | |
576 if( http_hdr->last_field==NULL ) { | |
577 http_hdr->first_field = new_field; | |
578 } else { | |
579 http_hdr->last_field->next = new_field; | |
580 } | |
581 http_hdr->last_field = new_field; | |
870 | 582 http_hdr->field_nb++; |
583 } | |
584 | |
585 void | |
586 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
587 if( http_hdr==NULL || method==NULL ) return; | |
588 | |
18879 | 589 http_hdr->method = malloc(strlen(method)+1); |
870 | 590 if( http_hdr->method==NULL ) { |
5915 | 591 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 592 return; |
593 } | |
594 strcpy( http_hdr->method, method ); | |
595 } | |
596 | |
597 void | |
598 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
599 if( http_hdr==NULL || uri==NULL ) return; | |
600 | |
18879 | 601 http_hdr->uri = malloc(strlen(uri)+1); |
870 | 602 if( http_hdr->uri==NULL ) { |
5915 | 603 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
870 | 604 return; |
605 } | |
606 strcpy( http_hdr->uri, uri ); | |
607 } | |
608 | |
6514 | 609 int |
610 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
611 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL; |
6514 | 612 int encoded_len, pass_len=0, out_len; |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
613 int res = -1; |
6514 | 614 if( http_hdr==NULL || username==NULL ) return -1; |
615 | |
616 if( password!=NULL ) { | |
617 pass_len = strlen(password); | |
618 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
619 |
18879 | 620 usr_pass = malloc(strlen(username)+pass_len+2); |
6514 | 621 if( usr_pass==NULL ) { |
622 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
623 goto out; |
6514 | 624 } |
625 | |
626 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
627 | |
628 // Base 64 encode with at least 33% more data than the original size | |
629 encoded_len = strlen(usr_pass)*2; | |
18879 | 630 b64_usr_pass = malloc(encoded_len); |
6514 | 631 if( b64_usr_pass==NULL ) { |
632 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
633 goto out; |
6514 | 634 } |
635 | |
636 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
637 if( out_len<0 ) { | |
638 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
639 goto out; |
6514 | 640 } |
641 | |
642 b64_usr_pass[out_len]='\0'; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
643 |
18879 | 644 auth = malloc(encoded_len+22); |
6514 | 645 if( auth==NULL ) { |
646 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
647 goto out; |
6514 | 648 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
649 |
6514 | 650 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); |
651 http_set_field( http_hdr, auth ); | |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
652 res = 0; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
653 |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
654 out: |
6514 | 655 free( usr_pass ); |
656 free( b64_usr_pass ); | |
657 free( auth ); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
658 |
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
659 return res; |
6514 | 660 } |
661 | |
870 | 662 void |
663 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
3039 | 664 HTTP_field_t *field; |
665 int i = 0; | |
902 | 666 if( http_hdr==NULL ) return; |
870 | 667 |
5915 | 668 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
669 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
670 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
671 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
672 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
673 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
674 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
675 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
870 | 676 |
5915 | 677 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
3039 | 678 field = http_hdr->first_field; |
679 while( field!=NULL ) { | |
5915 | 680 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
3039 | 681 field = field->next; |
682 } | |
5915 | 683 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
870 | 684 } |
6514 | 685 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
686 int |
6514 | 687 base64_encode(const void *enc, int encLen, char *out, int outMax) { |
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
688 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
6514 | 689 |
690 unsigned char *encBuf; | |
691 int outLen; | |
692 unsigned int bits; | |
693 unsigned int shift; | |
694 | |
695 encBuf = (unsigned char*)enc; | |
696 outLen = 0; | |
697 bits = 0; | |
698 shift = 0; | |
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
699 outMax &= ~3; |
6514 | 700 |
22031 | 701 while(1) { |
6514 | 702 if( encLen>0 ) { |
703 // Shift in byte | |
704 bits <<= 8; | |
705 bits |= *encBuf; | |
706 shift += 8; | |
707 // Next byte | |
708 encBuf++; | |
709 encLen--; | |
710 } else if( shift>0 ) { | |
711 // Pad last bits to 6 bits - will end next loop | |
712 bits <<= 6 - shift; | |
713 shift = 6; | |
714 } else { | |
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
715 // As per RFC 2045, section 6.8, |
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
716 // pad output as necessary: 0 to 2 '=' chars. |
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
717 while( outLen & 3 ){ |
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
718 *out++ = '='; |
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
719 outLen++; |
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
720 } |
6514 | 721 |
722 return outLen; | |
723 } | |
724 | |
725 // Encode 6 bit segments | |
726 while( shift>=6 ) { | |
22031 | 727 if (outLen >= outMax) |
728 return -1; | |
6514 | 729 shift -= 6; |
730 *out = b64[ (bits >> shift) & 0x3F ]; | |
731 out++; | |
732 outLen++; | |
733 } | |
734 } | |
735 } | |
736 | |
25970
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
737 static void print_icy_metadata(HTTP_header_t *http_hdr) { |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
738 const char *field_data; |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
739 // note: I skip icy-notice1 and 2, as they contain html <BR> |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
740 // and are IMHO useless info ::atmos |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
741 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
742 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
743 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
744 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
745 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
746 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
747 // XXX: does this really mean public server? ::atmos |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
748 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
749 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
750 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
751 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
752 } |
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
753 |
21567 | 754 //! If this function succeeds you must closesocket stream->fd |
15585 | 755 static int http_streaming_start(stream_t *stream, int* file_format) { |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
756 HTTP_header_t *http_hdr = NULL; |
15585 | 757 unsigned int i; |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
758 int fd = stream->fd; |
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
759 int res = STREAM_UNSUPPORTED; |
15585 | 760 int redirect = 0; |
761 int auth_retry=0; | |
762 int seekable=0; | |
763 char *content_type; | |
30357
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
764 const char *content_length; |
15585 | 765 char *next_url; |
766 URL_t *url = stream->streaming_ctrl->url; | |
6514 | 767 |
15585 | 768 do |
769 { | |
21541
3b4ed8857b38
Fix potential endless loop in http_streaming_start due
reimar
parents:
21540
diff
changeset
|
770 redirect = 0; |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
771 if (fd > 0) closesocket(fd); |
15585 | 772 fd = http_send_request( url, 0 ); |
773 if( fd<0 ) { | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
774 goto err_out; |
15585 | 775 } |
776 | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
777 http_free(http_hdr); |
15585 | 778 http_hdr = http_read_response( fd ); |
779 if( http_hdr==NULL ) { | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
780 goto err_out; |
15585 | 781 } |
782 | |
17932 | 783 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { |
15585 | 784 http_debug_hdr( http_hdr ); |
785 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
786 |
15585 | 787 // Check if we can make partial content requests and thus seek in http-streams |
788 if( http_hdr!=NULL && http_hdr->status_code==200 ) { | |
30106 | 789 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges"); |
30105
31b7d4637b50
Fix crash if http reply contains neither "Accept-Ranges" nor "Server" fields.
reimar
parents:
30103
diff
changeset
|
790 const char *server = http_get_field(http_hdr, "Server"); |
30106 | 791 if (accept_ranges) |
15585 | 792 seekable = strncmp(accept_ranges,"bytes",5)==0; |
30105
31b7d4637b50
Fix crash if http reply contains neither "Accept-Ranges" nor "Server" fields.
reimar
parents:
30103
diff
changeset
|
793 else if (server && strcmp(server, "gvs 1.0") == 0) |
30103
f7af93ece976
Add a hack for broken youtube servers not returning Accept-Ranges.
reimar
parents:
29920
diff
changeset
|
794 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking |
15585 | 795 } |
796 | |
25971
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
797 print_icy_metadata(http_hdr); |
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
798 |
15585 | 799 // Check if the response is an ICY status_code reason_phrase |
25968
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
800 if( !strcasecmp(http_hdr->protocol, "ICY") || |
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
801 http_get_field(http_hdr, "Icy-MetaInt") ) { |
15585 | 802 switch( http_hdr->status_code ) { |
803 case 200: { // OK | |
25969 | 804 char *field_data; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
805 // If content-type == video/nsv we most likely have a winamp video stream |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
806 // otherwise it should be mp3. if there are more types consider adding mime type |
15585 | 807 // handling like later |
808 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) | |
809 *file_format = DEMUXER_TYPE_NSV; | |
16948
9b7925705f5b
Add another content-type for aac audio in shoutcast streams
rtognimp
parents:
16932
diff
changeset
|
810 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) |
16917
c45409728a9d
Use correct demuxer type for aac in shoutcast streams
rtognimp
parents:
16614
diff
changeset
|
811 *file_format = DEMUXER_TYPE_AAC; |
15585 | 812 else |
813 *file_format = DEMUXER_TYPE_AUDIO; | |
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
814 res = STREAM_ERROR; |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
815 goto out; |
15585 | 816 } |
817 case 400: // Server Full | |
818 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
819 goto err_out; |
15585 | 820 case 401: // Service Unavailable |
821 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
822 goto err_out; |
15585 | 823 case 403: // Service Forbidden |
824 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
825 goto err_out; |
15585 | 826 case 404: // Resource Not Found |
827 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
828 goto err_out; |
15585 | 829 default: |
830 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
831 goto err_out; |
15585 | 832 } |
833 } | |
834 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
835 // Assume standard http if not ICY |
15585 | 836 switch( http_hdr->status_code ) { |
837 case 200: // OK | |
30357
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
838 content_length = http_get_field(http_hdr, "Content-Length"); |
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
839 if (content_length) { |
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
840 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length); |
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
841 stream->end_pos = atoll(content_length); |
3fdf04500df2
Handle Content-Length also when Content-Type is not set.
reimar
parents:
30356
diff
changeset
|
842 } |
15585 | 843 // Look if we can use the Content-Type |
844 content_type = http_get_field( http_hdr, "Content-Type" ); | |
845 if( content_type!=NULL ) { | |
846 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); | |
847 // Check in the mime type table for a demuxer type | |
848 i = 0; | |
849 while(mime_type_table[i].mime_type != NULL) { | |
850 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
851 *file_format = mime_type_table[i].demuxer_type; | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
852 res = seekable; |
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
853 goto out; |
15585 | 854 } |
855 i++; | |
856 } | |
857 } | |
858 // Not found in the mime type table, don't fail, | |
859 // we should try raw HTTP | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
860 res = seekable; |
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
861 goto out; |
15585 | 862 // Redirect |
863 case 301: // Permanently | |
864 case 302: // Temporarily | |
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
865 case 303: // See Other |
15585 | 866 // TODO: RFC 2616, recommand to detect infinite redirection loops |
867 next_url = http_get_field( http_hdr, "Location" ); | |
868 if( next_url!=NULL ) { | |
25238
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
869 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
20784
720206eef78b
Support URL redirections that do not specify full URL.
reimar
parents:
20185
diff
changeset
|
870 stream->streaming_ctrl->url = url_redirect( &url, next_url ); |
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
871 if (!strcasecmp(url->protocol, "mms")) { |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
872 res = STREAM_REDIRECTED; |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
873 goto err_out; |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
874 } |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
875 if (strcasecmp(url->protocol, "http")) { |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
876 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol); |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
877 goto err_out; |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
878 } |
25238
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
879 if (is_ultravox) { |
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
880 free(url->protocol); |
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
881 url->protocol = strdup("unsv"); |
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
882 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
883 redirect = 1; |
15585 | 884 } |
885 break; | |
886 case 401: // Authentication required | |
21566
79d969f9eec0
STREAM_UNSUPPORTED is -1, so use the former for return value in all places.
reimar
parents:
21565
diff
changeset
|
887 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
888 goto err_out; |
15585 | 889 redirect = 1; |
890 break; | |
891 default: | |
892 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
893 goto err_out; |
15585 | 894 } |
895 } while( redirect ); | |
896 | |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
897 err_out: |
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
898 if (fd > 0) closesocket( fd ); |
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
899 fd = -1; |
21542 | 900 http_free( http_hdr ); |
21776 | 901 http_hdr = NULL; |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
902 out: |
21776 | 903 stream->streaming_ctrl->data = (void*)http_hdr; |
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
904 stream->fd = fd; |
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
905 return res; |
15585 | 906 } |
907 | |
908 static int fixup_open(stream_t *stream,int seekable) { | |
16013 | 909 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; |
16078
095e980cf7c0
Some ICY servers (e.g. http://broadcast.spnet.net:8000/darikhigh) do not set
reimar
parents:
16070
diff
changeset
|
910 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); |
16070 | 911 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
15585 | 912 |
913 stream->type = STREAMTYPE_STREAM; | |
16013 | 914 if(!is_icy && !is_ultravox && seekable) |
15585 | 915 { |
29920
4f740437ed2b
Finally rename the STREAM_SEEK define to MP_STREAM_SEEK, there are just too many
reimar
parents:
29263
diff
changeset
|
916 stream->flags |= MP_STREAM_SEEK; |
15585 | 917 stream->seek = http_seek; |
918 } | |
919 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
16013 | 920 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) |
15585 | 921 if(nop_streaming_start( stream )) { |
922 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
21567 | 923 if (stream->fd >= 0) |
924 closesocket(stream->fd); | |
925 stream->fd = -1; | |
15585 | 926 streaming_ctrl_free(stream->streaming_ctrl); |
927 stream->streaming_ctrl = NULL; | |
24257 | 928 return STREAM_UNSUPPORTED; |
15585 | 929 } |
930 | |
931 fixup_network_stream_cache(stream); | |
932 return STREAM_OK; | |
933 } | |
934 | |
935 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
936 int seekable=0; | |
937 URL_t *url; | |
938 | |
939 stream->streaming_ctrl = streaming_ctrl_new(); | |
940 if( stream->streaming_ctrl==NULL ) { | |
941 return STREAM_ERROR; | |
942 } | |
943 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
944 url = url_new(stream->url); | |
945 stream->streaming_ctrl->url = check4proxies(url); | |
16417 | 946 url_free(url); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
947 |
20185 | 948 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url); |
15585 | 949 seekable = http_streaming_start(stream, file_format); |
950 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
21567 | 951 if (stream->fd >= 0) |
952 closesocket(stream->fd); | |
953 stream->fd = -1; | |
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
954 if (seekable == STREAM_REDIRECTED) |
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
955 return seekable; |
15585 | 956 streaming_ctrl_free(stream->streaming_ctrl); |
957 stream->streaming_ctrl = NULL; | |
24257 | 958 return STREAM_UNSUPPORTED; |
15585 | 959 } |
960 | |
961 return fixup_open(stream, seekable); | |
962 } | |
963 | |
964 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
965 int seekable=0; | |
966 URL_t *url; | |
967 | |
968 stream->streaming_ctrl = streaming_ctrl_new(); | |
969 if( stream->streaming_ctrl==NULL ) { | |
970 return STREAM_ERROR; | |
971 } | |
972 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
973 url = url_new(stream->url); | |
974 stream->streaming_ctrl->url = check4proxies(url); | |
16614 | 975 url_free(url); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
28402
diff
changeset
|
976 |
20185 | 977 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url); |
15585 | 978 seekable = http_streaming_start(stream, file_format); |
979 if(seekable < 0) { | |
21567 | 980 if (stream->fd >= 0) |
981 closesocket(stream->fd); | |
982 stream->fd = -1; | |
15585 | 983 streaming_ctrl_free(stream->streaming_ctrl); |
984 stream->streaming_ctrl = NULL; | |
24257 | 985 return STREAM_UNSUPPORTED; |
15585 | 986 } |
987 | |
988 return fixup_open(stream, seekable); | |
989 } | |
990 | |
991 | |
25211 | 992 const stream_info_t stream_info_http1 = { |
15585 | 993 "http streaming", |
994 "null", | |
995 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
996 "plain http", | |
997 open_s1, | |
27847
28deb37052cd
Add a noicyx:// protocol to allow easier testing for misconfigured servers.
reimar
parents:
27834
diff
changeset
|
998 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL}, |
15585 | 999 NULL, |
1000 0 // Urls are an option string | |
1001 }; | |
1002 | |
25211 | 1003 const stream_info_t stream_info_http2 = { |
15585 | 1004 "http streaming", |
1005 "null", | |
1006 "Bertrand, Albeu, Arpi? who?", | |
16932
c30e0970250c
fix typos: aslo->also, falback->fallback (they were just too annoying *g*)
reimar
parents:
16917
diff
changeset
|
1007 "plain http, also used as fallback for many other protocols", |
15585 | 1008 open_s2, |
1009 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
1010 NULL, | |
1011 0 // Urls are an option string | |
1012 }; |