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