summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/input/httpInputStream.cpp
blob: df2f09c4be133a16b9cf79513a6ac7b9c2a7b1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
  reads input data
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "httpInputStream.h"

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#include <iostream>

using namespace std;

static const char *httpstr="http://";



static char *strndup(char *src,int num) {
  char *dst;

  if(!(dst=(char *)malloc(num+1)))return NULL;
  dst[num]='\0';

  return strncpy(dst, src, num);
}

static char *url2hostport(char *url,char **hname,
			  unsigned long *hip,unsigned int *port) {
  char *cptr;
  struct hostent *myhostent;
  struct in_addr myaddr;
  int isip=1;

  if(!(strncmp(url,httpstr,7)))url+=7;
  cptr=url;
  while(*cptr && *cptr!=':' && *cptr!='/') {
    if((*cptr<'0' || *cptr>'9') && *cptr!='.')isip=0;
    cptr++;
  }
  if(!(*hname=strndup(url,cptr-url))) {
    *hname=NULL;
    return NULL;
  }
  if(!isip)
  {
    if (!(myhostent=gethostbyname(*hname)))return NULL;
    memcpy(&myaddr,myhostent->h_addr,sizeof(myaddr));
    *hip=myaddr.s_addr;
  }
  else if((*hip=inet_addr(*hname))==INADDR_NONE)return NULL;
  if(!*cptr || *cptr=='/') {
    *port=80;
    return cptr;
  }
  *port=atoi(++cptr);
  while(*cptr && *cptr!='/')cptr++;
  return cptr;
}













HttpInputStream::HttpInputStream() {

  proxyurl=NULL;
  proxyip=0;
  lopen=false;
  byteCnt=0;
}


HttpInputStream::~HttpInputStream() {
  close();
}


int HttpInputStream::open(const char* filename) {
  close();
  if (filename == NULL) {
    return false;
  }
  /*
  int matchPos=InputStream::getPath(filename,"http");
  if (matchPos=0) {
    return false;
  }
  */

  char* filename2=strdup(filename);
  if((fp=http_open(filename2))==NULL) {
    cout << "seterrorcode(SOUND_ERROR_FILEOPENFAIL)"<<endl;
    delete filename2;
    return false;
  }
  delete filename2;
  lopen=true;
  setUrl(filename);
  return lopen;
}


void HttpInputStream::close() {
  if (isOpen()) {
    ::fclose(fp);
  }
  lopen=false;
  setUrl(NULL);
}


int HttpInputStream::isOpen() {
  return lopen;
}


int HttpInputStream::eof() {
  if (isOpen()==false){
    return true;
  }
  return feof(fp);
}


int HttpInputStream::read(char* ptr,int size) {
  int bytesRead=0;
  if (isOpen()) {
    bytesRead=fread(ptr, 1,size, fp);
    if (ferror(fp) != 0){
      cout <<"http fread error"<<endl;
    } else {
      byteCnt+=bytesRead;
    }
  }
  return bytesRead;
}

int HttpInputStream::seek(long posInBytes) {
  cout << "HttpInputStream::setBytePos not implemented:"<<posInBytes<<endl;
  return false;
}



long HttpInputStream::getByteLength() {
  cout << "HttpInputStream::getByteLength not implemented"<<endl;
  return 0;
}



void HttpInputStream::print() {
  printf("pos in file:%8x\n",(int)ftell(fp));
}


int HttpInputStream::writestring(int fd, char *string) {
  int result,bytes=strlen(string);

  while (bytes) {
    if((result=SOCKETWRITEFUNC(fd,string,bytes))<0 && errno!=EINTR) {
      cout << "writestring fail -1"<<endl;
      return false;
    }
    else if(result==0) {
      cout << "writestring fail -2"<<endl;
      return false;
    }
    string += result;
    bytes -= result;
  }
  return true;
}



FILE* HttpInputStream::http_open(char *url) {
  char *purl=NULL,*host,*request,*sptr;
  char agent[50];
  int linelength;
  unsigned long myip;
  unsigned int myport;
  int sock;
  int relocate=0,numrelocs=0;
  struct sockaddr_in server;
  FILE *myfile;
  if (url == NULL) {
    cout << "cannot open NULL http_open"<<endl;
    return NULL;
  }
  if (strlen(url)==0) {
    cout << "zero length http_open"<<endl;
    return 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)))
      {
	cout << "seterrorcode(SOUND_ERROR_UNKNOWNPROXY)"<<endl;;
	return NULL;
      }
      if(host)free(host);
    }
    else
      proxyip = INADDR_NONE;
  }
  
  if((linelength=strlen(url)+100)<1024)
    linelength=1024;
  if(!(request=(char *)malloc(linelength)) || !(purl=(char *)malloc(1024))) 
  {
    cout << "seterrorcode(SOUND_ERROR_MEMORYNOTENOUGH)"<<endl;
    return NULL;
  }
  strncpy(purl,url,1023);
  purl[1023]='\0';
  do{
    strcpy(request,"GET ");
    if(proxyip!=INADDR_NONE) 
    {
      if(strncmp(url,httpstr,7))
	strcat(request,httpstr);
      strcat(request,purl);
      myport=proxyport;
      myip=proxyip;
    }
    else
    {
      if(!(sptr=url2hostport(purl,&host,&myip,&myport)))
      {
	cout << "seterrorcode(SOUND_ERROR_UNKNOWNHOST)"<<endl;;
	return NULL;
      }
      if (host)
	free (host);
      strcat (request, sptr);
    }
    sprintf (agent, " HTTP/1.0\r\nUser-Agent: %s/%s\r\n\r\n",
	     "Splay","0.6");
    strcat (request, agent);
    server.sin_family = AF_INET;
    server.sin_port = htons(myport);
    server.sin_addr.s_addr = myip;
    if((sock=socket(PF_INET,SOCK_STREAM,6))<0) {
      cout <<"seterrorcode(SOUND_ERROR_SOCKET)"<<endl;
      return NULL;
    }
    if(connect(sock,(struct sockaddr *)&server,sizeof(server))) {
      cout <<"seterrorcode(SOUND_ERROR_CONNECT)"<<endl;
      return NULL;
    }
    if(!writestring(sock,request))return NULL;
    if(!(myfile=::fdopen(sock, "rb"))) {
      cout << "seterrorcode(SOUND_ERROR_FDOPEN)"<<endl;
      return NULL;
    };
    relocate=false;
    purl[0]='\0';
    if(!readstring(request,linelength-1,myfile))return NULL;
    if((sptr=strchr(request,' '))) {
      switch(sptr[1]) {
        case '3':relocate=true;
        case '2':break;
        default: 
	  cout <<"seterrorcode(SOUND_ERROR_HTTPFAIL)"<<endl;
	  return NULL;
      }
    }
    do {
      if(!readstring(request,linelength-1,myfile))return NULL;
      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) { 
    cout << "seterrorcode(SOUND_ERROR_TOOMANYRELOC)"<<endl;
    return NULL;
  }
  free(purl);
  free(request);
  return myfile;
}

long HttpInputStream::getBytePosition() {
  return 0;
}

int HttpInputStream::readstring(char *string,int maxlen,FILE *f) {
  char *result;

  do{
    result=fgets(string,maxlen,f);
  }while(!result  && errno==EINTR);
  if(!result)
  {
    cout << "seterrorcode(SOUND_ERROR_FILEREADFAIL)"<<endl;
    return false;
  }

  return true;
}