Audio-Play-MPG123
view release on metacpan or search on metacpan
mpg123/httpget.c view on Meta::CPAN
r_hostptr = p - 1;
}
if (*hostptr == '[' && *r_hostptr == ']') {
hostptr++;
r_hostptr--;
}
stringlength = r_hostptr - hostptr + 1;
h = malloc(stringlength + 1); /* removed the strndup for better portability */
if (h == NULL) {
*hname = NULL;
*port = NULL;
return NULL;
}
strncpy(h, hostptr, stringlength);
*(h+stringlength) = '\0';
*hname = h;
if (portptr) {
stringlength = (pathptr - portptr);
if(!stringlength) portptr = NULL;
}
if (portptr == NULL) {
portptr = defaultportstr;
stringlength = strlen(defaultportstr);
}
p0 = malloc(stringlength + 1);
if (p0 == NULL) {
free(h);
*hname = NULL;
*port = NULL;
return NULL;
}
strncpy(p0, portptr, stringlength);
*(p0 + stringlength) = '\0';
for (p = p0; *p && isdigit((unsigned char) *p); p++) ;
*p = '\0';
*port = (unsigned char *) p0;
return pathptr;
}
char *proxyurl = NULL;
unsigned long proxyip = 0;
unsigned char *proxyport;
#define ACCEPT_HEAD "Accept: audio/mpeg, audio/x-mpegurl, */*\r\n"
char *httpauth = NULL;
char httpauth1[256];
int http_open (char *url)
{
char *purl, *host, *request, *sptr;
int linelength;
unsigned long myip;
unsigned char *myport;
int sock;
int relocate, numrelocs = 0;
FILE *myfile;
#ifdef INET6
struct addrinfo hints, *res, *res0;
int error;
#else
struct hostent *hp;
struct sockaddr_in sin;
#endif
host = NULL;
proxyport = NULL;
myport = NULL;
if (!proxyip) {
if (!proxyurl)
if (!(proxyurl = getenv("MP3_HTTP_PROXY")))
if (!(proxyurl = getenv("http_proxy")))
proxyurl = getenv("HTTP_PROXY");
if (proxyurl && proxyurl[0] && strcmp(proxyurl, "none")) {
if (!(url2hostport(proxyurl, &host, &proxyip, &proxyport))) {
fprintf (stderr, "Unknown proxy host \"%s\".\n",
host ? host : "");
exit (1);
}
#if 0
if (host)
free (host);
#endif
}
else
proxyip = INADDR_NONE;
}
if (proxyip == INADDR_NONE)
if (strncasecmp(url, "ftp://", 6) == 0){
fprintf(stderr,"Downloading from ftp servers without PROXY not allowed\n");
exit(1);
}
if ((linelength = strlen(url)+200) < 1024)
linelength = 1024;
if (!(request = malloc(linelength)) || !(purl = malloc(1024))) {
fprintf (stderr, "malloc() failed, out of memory.\n");
exit (1);
}
/*
* 2000-10-21:
* We would like spaces to be automatically converted to %20's when
* fetching via HTTP.
* -- Martin Sjögren <md9ms@mdstud.chalmers.se>
*/
if ((sptr = strchr(url, ' ')) == NULL) {
strncpy (purl, url, 1023);
purl[1023] = '\0';
}
else {
int purllength = 0;
char *urlptr = url;
purl[0] = '\0';
mpg123/httpget.c view on Meta::CPAN
}
sock = -1;
for (res = res0; res; res = res->ai_next) {
if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
continue;
}
if (connect(sock, res->ai_addr, res->ai_addrlen)) {
close(sock);
sock = -1;
continue;
}
break;
}
freeaddrinfo(res0);
#else
sock = -1;
hp = gethostbyname(host);
if (!hp)
goto fail;
if (hp->h_length != sizeof(sin.sin_addr))
goto fail;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
goto fail;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
/* sin.sin_len = sizeof(struct sockaddr_in); */
memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
sin.sin_port = htons(atoi( (char *) myport));
if (connect(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in) ) < 0) {
close(sock);
sock = -1;
}
fail:
#endif
if (sock < 0) {
perror("socket");
exit(1);
}
if (strlen(httpauth1) || httpauth) {
char buf[1023];
strcat (request,"Authorization: Basic ");
if(strlen(httpauth1))
encode64(httpauth1,buf);
else
encode64(httpauth,buf);
strcat (request,buf);
strcat (request,"\r\n");
}
strcat (request, "\r\n");
writestring (sock, request);
if (!(myfile = fdopen(sock, "rb"))) {
perror ("fdopen");
exit (1);
};
relocate = FALSE;
purl[0] = '\0';
readstring (request, linelength-1, myfile);
if ((sptr = strchr(request, ' '))) {
switch (sptr[1]) {
case '3':
relocate = TRUE;
case '2':
break;
default:
fprintf (stderr, "HTTP request failed: %s",
sptr+1); /* '\n' is included */
exit (1);
}
}
do {
readstring (request, linelength-1, myfile);
if (!strncmp(request, "Location:", 9))
strncpy (purl, request+10, 1023);
} while (request[0] != '\r' && request[0] != '\n');
} while (relocate && purl[0] && numrelocs++ < 5);
if (relocate) {
fprintf (stderr, "Too many HTTP relocations.\n");
exit (1);
}
free (purl);
free (request);
free(host);
free(proxyport);
free(myport);
return sock;
}
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern int errno;
#include "mpg123.h"
void writestring (int fd, char *string)
{
}
void readstring (char *string, int maxlen, FILE *f)
{
}
char *url2hostport (char *url, char **hname, unsigned long *hip, unsigned int *port)
{
}
char *proxyurl = NULL;
unsigned long proxyip = 0;
unsigned int proxyport;
#define ACCEPT_HEAD "Accept: audio/mpeg, audio/x-mpegurl, */*\r\n"
int http_open (char *url)
{
}
#endif
/* EOF */
( run in 2.985 seconds using v1.01-cache-2.11-cpan-800906f7e73 )