comparison libmpdemux/network.c @ 4121:a71d4ffc6c97

Added proxy support.
author bertrand
date Sat, 12 Jan 2002 21:08:12 +0000
parents eac2948c00d4
children c66fddd8867c
comparison
equal deleted inserted replaced
4120:a66048f5ae2e 4121:a71d4ffc6c97
22 #include "network.h" 22 #include "network.h"
23 #include "http.h" 23 #include "http.h"
24 #include "url.h" 24 #include "url.h"
25 #include "asf.h" 25 #include "asf.h"
26 #include "rtp.h" 26 #include "rtp.h"
27
28 extern int verbose;
27 29
28 static struct { 30 static struct {
29 char *mime_type; 31 char *mime_type;
30 int demuxer_type; 32 int demuxer_type;
31 } mime_type_table[] = { 33 } mime_type_table[] = {
172 } 174 }
173 175
174 int 176 int
175 http_send_request( URL_t *url ) { 177 http_send_request( URL_t *url ) {
176 HTTP_header_t *http_hdr; 178 HTTP_header_t *http_hdr;
179 URL_t *server_url;
177 char str[80]; 180 char str[80];
178 int fd; 181 int fd;
182 int ret;
183 int proxy = 0; // Boolean
184
179 http_hdr = http_new_header(); 185 http_hdr = http_new_header();
180 http_set_uri( http_hdr, url->file ); 186
181 snprintf(str, 80, "Host: %s", url->hostname ); 187 if( !strcasecmp(url->protocol, "proxy") ) {
188 proxy = 1;
189 server_url = url_new( (url->file)+1 );
190 http_set_uri( http_hdr, server_url->url );
191 } else {
192 server_url = url;
193 http_set_uri( http_hdr, server_url->file );
194 }
195 snprintf(str, 80, "Host: %s", server_url->hostname );
182 http_set_field( http_hdr, str); 196 http_set_field( http_hdr, str);
183 http_set_field( http_hdr, "User-Agent: MPlayer"); 197 http_set_field( http_hdr, "User-Agent: MPlayer");
184 http_set_field( http_hdr, "Connection: closed"); 198 http_set_field( http_hdr, "Connection: closed");
185 if( http_build_request( http_hdr )==NULL ) { 199 if( http_build_request( http_hdr )==NULL ) {
186 return -1; 200 return -1;
187 } 201 }
188 202
189 if( url->port==0 ) url->port = 80; 203 if( proxy ) {
190 fd = connect2Server( url->hostname, url->port ); 204 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
205 fd = connect2Server( url->hostname, url->port );
206 url_free( server_url );
207 } else {
208 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
209 fd = connect2Server( server_url->hostname, server_url->port );
210 }
191 if( fd<0 ) { 211 if( fd<0 ) {
192 return -1; 212 return -1;
193 } 213 }
194 write( fd, http_hdr->buffer, http_hdr->buffer_size ); 214 if( verbose ) {
215 printf("Request: [%s]\n", http_hdr->buffer );
216 }
217
218 ret = write( fd, http_hdr->buffer, http_hdr->buffer_size );
219 if( ret!=http_hdr->buffer_size ) {
220 printf("Error while sending HTTP request: didn't sent all the request\n");
221 return -1;
222 }
223
195 http_free( http_hdr ); 224 http_free( http_hdr );
196 225
197 return fd; 226 return fd;
198 } 227 }
199 228
294 *file_format = DEMUXER_TYPE_ASF; 323 *file_format = DEMUXER_TYPE_ASF;
295 return 0; 324 return 0;
296 } 325 }
297 326
298 // HTTP based protocol 327 // HTTP based protocol
299 if( !strcasecmp(url->protocol, "http") ) { 328 if( !strcasecmp(url->protocol, "http") || !strcasecmp(url->protocol, "proxy") ) {
300 if( url->port==0 ) url->port = 80; 329 //if( url->port==0 ) url->port = 80;
301 330
302 fd = http_send_request( url ); 331 fd = http_send_request( url );
303 if( fd<0 ) { 332 if( fd<0 ) {
304 return -1; 333 return -1;
305 } 334 }
310 http_free( http_hdr ); 339 http_free( http_hdr );
311 return -1; 340 return -1;
312 } 341 }
313 342
314 *fd_out=fd; 343 *fd_out=fd;
315 http_debug_hdr( http_hdr ); 344 if( verbose ) {
316 345 http_debug_hdr( http_hdr );
346 }
347
317 streaming_ctrl->data = (void*)http_hdr; 348 streaming_ctrl->data = (void*)http_hdr;
318 349
319 // Check if the response is an ICY status_code reason_phrase 350 // Check if the response is an ICY status_code reason_phrase
320 if( !strcasecmp(http_hdr->protocol, "ICY") ) { 351 if( !strcasecmp(http_hdr->protocol, "ICY") ) {
321 // Ok, we have detected an mp3 streaming 352 // Ok, we have detected an mp3 streaming
580 streaming_start(stream_t *stream, int demuxer_type) { 611 streaming_start(stream_t *stream, int demuxer_type) {
581 int ret=-1; 612 int ret=-1;
582 if( stream==NULL ) return -1; 613 if( stream==NULL ) return -1;
583 614
584 // For RTP streams, we usually don't know the stream type until we open it. 615 // For RTP streams, we usually don't know the stream type until we open it.
585 if( !strcmp( stream->streaming_ctrl->url->protocol, "rtp")) { 616 if( !strcasecmp( stream->streaming_ctrl->url->protocol, "rtp")) {
586 if(stream->fd >= 0) { 617 if(stream->fd >= 0) {
587 if(close(stream->fd) < 0) 618 if(close(stream->fd) < 0)
588 printf("streaming_start : Closing socket %d failed %s\n",stream->fd,strerror(errno)); 619 printf("streaming_start : Closing socket %d failed %s\n",stream->fd,strerror(errno));
589 } 620 }
590 stream->fd = -1; 621 stream->fd = -1;