This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
drvrnet.c
4445 lines (3831 loc) · 123 KB
/
drvrnet.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file, drvrhttp.c contains driver routines for http, ftp and root
files. */
/* This file was written by Bruce O'Neel at the ISDC, Switzerland */
/* The FITSIO software is maintained by William Pence at the High Energy */
/* Astrophysic Science Archive Research Center (HEASARC) at the NASA */
/* Goddard Space Flight Center. */
/* Notes on the drivers:
The ftp driver uses passive mode exclusivly. If your remote system can't
deal with passive mode then it'll fail. Since Netscape Navigator uses
passive mode as well there shouldn't be too many ftp servers which have
problems.
The http driver works properly with 301 and 302 redirects. For many more
gory details see http://www.w3c.org/Protocols/rfc2068/rfc2068. The only
catch to the 301/302 redirects is that they have to redirect to another
http:// url. If not, things would have to change a lot in cfitsio and this
was thought to be too difficult.
Redirects look like
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://heasarc.gsfc.nasa.gov/FTP/software/ftools/release/other/image.fits.gz">here</A>.<P>
</BODY></HTML>
This redirect was from apache 1.2.5 but most of the other servers produce
something very similiar. The parser for the redirects finds the first
anchor <A> tag in the body and goes there. If that wasn't what was intended
by the remote system then hopefully the error stack, which includes notes
about the redirect will help the user fix the problem.
****************************************************************
Note added in 2017:
The redirect format shown above is actually preceded by 2 lines that look like
HTTP/1.1 302 Found
LOCATION: http://heasarc.gsfc.nasa.gov/FTP/software/ftools/release/other/image.fits.gz
The CFITSIO parser now looks for the "Location:" string, not the html tag.
****************************************************************
Root protocal doesn't have any real docs, so, the emperical docs are as
follows.
First, you must use a slightly modified rootd server. The modifications
include implimentation of the stat command which returns the size of the
remote file. Without that it's impossible for cfitsio to work properly
since fitsfiles don't include any information about the size of the files
in the headers. The rootd server closes the connections on any errors,
including reading beyond the end of the file or seeking beyond the end
of the file. The rootd:// driver doesn't reopen a closed connection, if
the connection is closed you're pretty much done.
The messages are of the form
<len><opcode><optional information>
All binary information is transfered in network format, so use htonl and
ntohl to convert back and forth.
<len> :== 4 byte length, in network format, the len doesn't include the
length of <len>
<opcode> :== one of the message opcodes below, 4 bytes, network format
<optional info> :== depends on opcode
The response is of the same form with the same opcode sent. Success is
indicated by <optional info> being 0.
Root is a NFSish protocol where each read/write includes the byte
offset to read or write to. As a result, seeks will always succeed
in the driver even if they would cause a fatal error when you try
to read because you're beyond the end of the file.
There is file locking on the host such that you need to possibly
create /usr/tmp/rootdtab on the host system. There is one file per
socket connection, though the rootd daemon can support multiple
files open at once.
The messages are sent in the following order:
ROOTD_USER - user name, <optional info> is the user name, trailing
null is sent though it's not required it seems. A ROOTD_AUTH
message is returned with any sort of error meaning that the user
name is wrong.
ROOTD_PASS - password, ones complemented, stored in <optional info>. Once
again the trailing null is sent. Once again a ROOTD_AUTH message is
returned
ROOTD_OPEN - <optional info> includes filename and one of
{create|update|read} as the file mode. ~ seems to be dealt with
as the username's login directory. A ROOTD_OPEN message is
returned.
Once the file is opened any of the following can be sent:
ROOTD_STAT - file status and size
returns a message where <optional info> is the file length in bytes
ROOTD_FLUSH - flushes the file, not sure this has any real effect
on the daemon since the daemon uses open/read/write/close rather
than the buffered fopen/fread/fwrite/fclose.
ROOTD_GET - on send <optional info> includes a text message of
offset and length to get. Return is a status message first with a
status value, then, the raw bytes for the length that you
requested. It's an error to seek or read past the end of the file,
and, the rootd daemon exits and won't respond anymore. Ie, don't
do this.
ROOTD_PUT - on send <optional info> includes a text message of
offset and length to put. Then send the raw bytes you want to
write. Then recieve a status message
When you are finished then you send the message:
ROOTD_CLOSE - closes the file
Once the file is closed then the socket is closed.
Revision 1.56 2000/01/04 11:58:31 oneel
Updates so that compressed network files are dealt with regardless of
their file names and/or mime types.
Revision 1.55 2000/01/04 10:52:40 oneel
cfitsio 2.034
Revision 1.51 1999/08/10 12:13:40 oneel
Make the http code a bit less picky about the types of files it
uncompresses. Now it also uncompresses files which end in .Z or .gz.
Revision 1.50 1999/08/04 12:38:46 oneel
Don's 2.0.32 patch with dal 1.3
Revision 1.39 1998/12/02 15:31:33 oneel
Updates to drvrnet.c so that less compiler warnings would be
generated. Fixes the signal handling.
Revision 1.38 1998/11/23 10:03:24 oneel
Added in a useragent string, as suggested by:
Tim Kimball Data Systems Division kimball@stsci.edu 410-338-4417
Space Telescope Science Institute http://www.stsci.edu/~kimball/
3700 San Martin Drive http://archive.stsci.edu/
Baltimore MD 21218 USA http://faxafloi.stsci.edu:4547/
*/
#ifdef HAVE_NET_SERVICES
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#ifdef CFITSIO_HAVE_CURL
#include <curl/curl.h>
#endif
#if defined(unix) || defined(__unix__) || defined(__unix) || defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include <signal.h>
#include <setjmp.h>
#include "fitsio2.h"
static jmp_buf env; /* holds the jump buffer for setjmp/longjmp pairs */
static void signal_handler(int sig);
/* Network routine error codes */
#define NET_OK 0
#define NOT_INET_ADDRESS -1000
#define UNKNOWN_INET_HOST -1001
#define CONNECTION_ERROR -1002
/* Network routine constants */
#define NET_DEFAULT 0
#define NET_OOB 1
#define NET_PEEK 2
/* local defines and variables */
#define MAXLEN 1200
#define SHORTLEN 100
static char netoutfile[MAXLEN];
#define ROOTD_USER 2000 /*user id follows */
#define ROOTD_PASS 2001 /*passwd follows */
#define ROOTD_AUTH 2002 /*authorization status (to client) */
#define ROOTD_FSTAT 2003 /*filename follows */
#define ROOTD_OPEN 2004 /*filename follows + mode */
#define ROOTD_PUT 2005 /*offset, number of bytes and buffer */
#define ROOTD_GET 2006 /*offset, number of bytes */
#define ROOTD_FLUSH 2007 /*flush file */
#define ROOTD_CLOSE 2008 /*close file */
#define ROOTD_STAT 2009 /*return rootd statistics */
#define ROOTD_ACK 2010 /*acknowledgement (all OK) */
#define ROOTD_ERR 2011 /*error code and message follow */
typedef struct /* structure containing disk file structure */
{
int sock;
LONGLONG currentpos;
} rootdriver;
typedef struct /* simple mem struct for receiving files from curl */
{
char *memory;
size_t size;
} curlmembuf;
static rootdriver handleTable[NMAXFILES]; /* allocate diskfile handle tables */
/* static prototypes */
static int NET_TcpConnect(char *hostname, int port);
static int NET_SendRaw(int sock, const void *buf, int length, int opt);
static int NET_RecvRaw(int sock, void *buffer, int length);
static int NET_ParseUrl(const char *url, char *proto, char *host, int *port,
char *fn);
static int CreateSocketAddress(struct sockaddr_in *sockaddrPtr,
char *host,int port);
static int ftp_status(FILE *ftp, char *statusstr);
static int http_open_network(char *url, FILE **httpfile, char *contentencoding,
int *contentlength);
static int https_open_network(char *filename, curlmembuf* buffer);
static int ftp_open_network(char *url, FILE **ftpfile, FILE **command,
int *sock);
static int ftps_open_network(char *filename, curlmembuf* buffer);
static int ftp_file_exist(char *url);
static int root_send_buffer(int sock, int op, char *buffer, int buflen);
static int root_recv_buffer(int sock, int *op, char *buffer,int buflen);
static int root_openfile(char *filename, char *rwmode, int *sock);
static int encode64(unsigned s_len, char *src, unsigned d_len, char *dst);
static int ssl_get_with_curl(char *url, curlmembuf* buffer,
char* username, char* password);
static size_t curlToMemCallback(void *buffer, size_t size, size_t nmemb, void *userp);
static int curlProgressCallback(void *clientp, double dltotal, double dlnow,
double ultotal, double ulnow);
/***************************/
/* Static variables */
static int closehttpfile;
static int closememfile;
static int closefdiskfile;
static int closediskfile;
static int closefile;
static int closeoutfile;
static int closecommandfile;
static int closeftpfile;
static FILE *diskfile;
static FILE *outfile;
static int curl_verbose=0;
static int show_fits_download_progress=0;
static unsigned int net_timeout = 360; /* in seconds */
/*--------------------------------------------------------------------------*/
/* This creates a memory file handle with a copy of the URL in filename. The
file is uncompressed if necessary */
int http_open(char *filename, int rwmode, int *handle)
{
FILE *httpfile;
char contentencoding[SHORTLEN];
char errorstr[MAXLEN];
char recbuf[MAXLEN];
long len;
int contentlength;
int status;
char firstchar;
closehttpfile = 0;
closememfile = 0;
/* don't do r/w files */
if (rwmode != 0) {
ffpmsg("Can't open http:// type file with READWRITE access");
ffpmsg(" Specify an outfile for r/w access (http_open)");
goto error;
}
/* do the signal handler bits */
if (setjmp(env) != 0) {
/* feels like the second time */
/* this means something bad happened */
ffpmsg("Timeout (http_open)");
snprintf(errorstr, MAXLEN, "Download timeout exceeded: %d seconds",net_timeout);
ffpmsg(errorstr);
ffpmsg(" (multiplied x10 for files requiring uncompression)");
ffpmsg(" Timeout may be adjusted with fits_set_timeout");
goto error;
}
(void) signal(SIGALRM, signal_handler);
/* Open the network connection */
if (http_open_network(filename,&httpfile,contentencoding,
&contentlength)) {
alarm(0);
ffpmsg("Unable to open http file (http_open):");
ffpmsg(filename);
goto error;
}
closehttpfile++;
/* Create the memory file */
if ((status = mem_create(filename,handle))) {
ffpmsg("Unable to create memory file (http_open)");
goto error;
}
closememfile++;
/* Now, what do we do with the file */
/* Check to see what the first character is */
firstchar = fgetc(httpfile);
ungetc(firstchar,httpfile);
if (!strcmp(contentencoding,"x-gzip") ||
!strcmp(contentencoding,"x-compress") ||
strstr(filename,".gz") ||
strstr(filename,".Z") ||
('\037' == firstchar)) {
/* do the compress dance, which is the same as the gzip dance */
/* Using the cfitsio routine */
status = 0;
/* Ok, this is a tough case, let's be arbritary and say 10*net_timeout,
Given the choices for nettimeout above they'll probaby ^C before, but
it's always worth a shot*/
alarm(net_timeout*10);
status = mem_uncompress2mem(filename, httpfile, *handle);
alarm(0);
if (status) {
ffpmsg("Error writing compressed memory file (http_open)");
ffpmsg(filename);
goto error;
}
} else {
/* It's not compressed, bad choice, but we'll copy it anyway */
if (contentlength % 2880) {
snprintf(errorstr,MAXLEN,"Content-Length not a multiple of 2880 (http_open) %d",
contentlength);
ffpmsg(errorstr);
}
/* write a memory file */
alarm(net_timeout);
while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) {
alarm(0); /* cancel alarm */
status = mem_write(*handle,recbuf,len);
if (status) {
ffpmsg("Error copying http file into memory (http_open)");
ffpmsg(filename);
goto error;
}
alarm(net_timeout); /* rearm the alarm */
}
}
fclose(httpfile);
signal(SIGALRM, SIG_DFL);
alarm(0);
return mem_seek(*handle,0);
error:
alarm(0); /* clear it */
if (closehttpfile) {
fclose(httpfile);
}
if (closememfile) {
mem_close_free(*handle);
}
signal(SIGALRM, SIG_DFL);
return (FILE_NOT_OPENED);
}
/*--------------------------------------------------------------------------*/
/* This creates a memory file handle with a copy of the URL in filename. The
file must be compressed and is copied (still compressed) to disk first.
The compressed disk file is then uncompressed into memory (READONLY).
*/
int http_compress_open(char *url, int rwmode, int *handle)
{
FILE *httpfile;
char contentencoding[SHORTLEN];
char errorstr[MAXLEN];
char recbuf[MAXLEN];
long len;
int contentlength;
int ii, flen, status;
char firstchar;
closehttpfile = 0;
closediskfile = 0;
closefdiskfile = 0;
closememfile = 0;
flen = strlen(netoutfile);
if (!flen) {
/* cfileio made a mistake, should set the netoufile first otherwise
we don't know where to write the output file */
ffpmsg
("Output file not set, shouldn't have happened (http_compress_open)");
goto error;
}
if (rwmode != 0) {
ffpmsg("Can't open compressed http:// type file with READWRITE access");
ffpmsg(" Specify an UNCOMPRESSED outfile (http_compress_open)");
goto error;
}
/* do the signal handler bits */
if (setjmp(env) != 0) {
/* feels like the second time */
/* this means something bad happened */
ffpmsg("Timeout (http_open)");
snprintf(errorstr, MAXLEN, "Download timeout exceeded: %d seconds",net_timeout);
ffpmsg(errorstr);
ffpmsg(" Timeout may be adjusted with fits_set_timeout");
goto error;
}
signal(SIGALRM, signal_handler);
/* Open the http connectin */
alarm(net_timeout);
if ((status = http_open_network(url,&httpfile,contentencoding,
&contentlength))) {
alarm(0);
ffpmsg("Unable to open http file (http_compress_open)");
ffpmsg(url);
goto error;
}
closehttpfile++;
/* Better be compressed */
firstchar = fgetc(httpfile);
ungetc(firstchar,httpfile);
if (!strcmp(contentencoding,"x-gzip") ||
!strcmp(contentencoding,"x-compress") ||
('\037' == firstchar)) {
if (*netoutfile == '!')
{
/* user wants to clobber file, if it already exists */
for (ii = 0; ii < flen; ii++)
netoutfile[ii] = netoutfile[ii + 1]; /* remove '!' */
status = file_remove(netoutfile);
}
/* Create the new file */
if ((status = file_create(netoutfile,handle))) {
ffpmsg("Unable to create output disk file (http_compress_open):");
ffpmsg(netoutfile);
goto error;
}
closediskfile++;
/* write a file */
alarm(net_timeout);
while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) {
alarm(0);
status = file_write(*handle,recbuf,len);
if (status) {
ffpmsg("Error writing disk file (http_compres_open)");
ffpmsg(netoutfile);
goto error;
}
alarm(net_timeout);
}
file_close(*handle);
fclose(httpfile);
closehttpfile--;
closediskfile--;
/* File is on disk, let's uncompress it into memory */
if (NULL == (diskfile = fopen(netoutfile,"r"))) {
ffpmsg("Unable to reopen disk file (http_compress_open)");
ffpmsg(netoutfile);
goto error;
}
closefdiskfile++;
/* Create the memory handle to hold it */
if ((status = mem_create(url,handle))) {
ffpmsg("Unable to create memory file (http_compress_open)");
goto error;
}
closememfile++;
/* Uncompress it */
status = 0;
status = mem_uncompress2mem(url,diskfile,*handle);
fclose(diskfile);
closefdiskfile--;
if (status) {
ffpmsg("Error uncompressing disk file to memory (http_compress_open)");
ffpmsg(netoutfile);
goto error;
}
} else {
/* Opps, this should not have happened */
ffpmsg("Can only have compressed files here (http_compress_open)");
goto error;
}
signal(SIGALRM, SIG_DFL);
alarm(0);
return mem_seek(*handle,0);
error:
alarm(0); /* clear it */
if (closehttpfile) {
fclose(httpfile);
}
if (closefdiskfile) {
fclose(diskfile);
}
if (closememfile) {
mem_close_free(*handle);
}
if (closediskfile) {
file_close(*handle);
}
signal(SIGALRM, SIG_DFL);
return (FILE_NOT_OPENED);
}
/*--------------------------------------------------------------------------*/
/* This creates a file handle with a copy of the URL in filename. The http
file is copied to disk first. If it's compressed then it is
uncompressed when copying to the disk */
int http_file_open(char *url, int rwmode, int *handle)
{
FILE *httpfile;
char contentencoding[SHORTLEN];
char errorstr[MAXLEN];
char recbuf[MAXLEN];
long len;
int contentlength;
int ii, flen, status;
char firstchar;
/* Check if output file is actually a memory file */
if (!strncmp(netoutfile, "mem:", 4) )
{
/* allow the memory file to be opened with write access */
return( http_open(url, READONLY, handle) );
}
closehttpfile = 0;
closefile = 0;
closeoutfile = 0;
flen = strlen(netoutfile);
if (!flen) {
/* cfileio made a mistake, we need to know where to write the file */
ffpmsg("Output file not set, shouldn't have happened (http_file_open)");
return (FILE_NOT_OPENED);
}
/* do the signal handler bits */
if (setjmp(env) != 0) {
/* feels like the second time */
/* this means something bad happened */
ffpmsg("Timeout (http_open)");
snprintf(errorstr, MAXLEN, "Download timeout exceeded: %d seconds",net_timeout);
ffpmsg(errorstr);
ffpmsg(" (multiplied x10 for files requiring uncompression)");
ffpmsg(" Timeout may be adjusted with fits_set_timeout");
goto error;
}
signal(SIGALRM, signal_handler);
/* Open the network connection */
alarm(net_timeout);
if ((status = http_open_network(url,&httpfile,contentencoding,
&contentlength))) {
alarm(0);
ffpmsg("Unable to open http file (http_file_open)");
ffpmsg(url);
goto error;
}
closehttpfile++;
if (*netoutfile == '!')
{
/* user wants to clobber disk file, if it already exists */
for (ii = 0; ii < flen; ii++)
netoutfile[ii] = netoutfile[ii + 1]; /* remove '!' */
status = file_remove(netoutfile);
}
firstchar = fgetc(httpfile);
ungetc(firstchar,httpfile);
if (!strcmp(contentencoding,"x-gzip") ||
!strcmp(contentencoding,"x-compress") ||
('\037' == firstchar)) {
/* to make this more cfitsioish we use the file driver calls to create
the disk file */
/* Create the output file */
if ((status = file_create(netoutfile,handle))) {
ffpmsg("Unable to create output file (http_file_open)");
ffpmsg(netoutfile);
goto error;
}
file_close(*handle);
if (NULL == (outfile = fopen(netoutfile,"w"))) {
ffpmsg("Unable to reopen the output file (http_file_open)");
ffpmsg(netoutfile);
goto error;
}
closeoutfile++;
status = 0;
/* Ok, this is a tough case, let's be arbritary and say 10*net_timeout,
Given the choices for nettimeout above they'll probaby ^C before, but
it's always worth a shot*/
alarm(net_timeout*10);
status = uncompress2file(url,httpfile,outfile,&status);
alarm(0);
if (status) {
ffpmsg("Error uncompressing http file to disk file (http_file_open)");
ffpmsg(url);
ffpmsg(netoutfile);
goto error;
}
fclose(outfile);
closeoutfile--;
} else {
/* Create the output file */
if ((status = file_create(netoutfile,handle))) {
ffpmsg("Unable to create output file (http_file_open)");
ffpmsg(netoutfile);
goto error;
}
/* Give a warning message. This could just be bad padding at the end
so don't treat it like an error. */
closefile++;
if (contentlength % 2880) {
snprintf(errorstr, MAXLEN,
"Content-Length not a multiple of 2880 (http_file_open) %d",
contentlength);
ffpmsg(errorstr);
}
/* write a file */
alarm(net_timeout);
while(0 != (len = fread(recbuf,1,MAXLEN,httpfile))) {
alarm(0);
status = file_write(*handle,recbuf,len);
if (status) {
ffpmsg("Error copying http file to disk file (http_file_open)");
ffpmsg(url);
ffpmsg(netoutfile);
goto error;
}
}
file_close(*handle);
closefile--;
}
fclose(httpfile);
closehttpfile--;
signal(SIGALRM, SIG_DFL);
alarm(0);
return file_open(netoutfile,rwmode,handle);
error:
alarm(0); /* clear it */
if (closehttpfile) {
fclose(httpfile);
}
if (closeoutfile) {
fclose(outfile);
}
if (closefile) {
file_close(*handle);
}
signal(SIGALRM, SIG_DFL);
return (FILE_NOT_OPENED);
}
/*--------------------------------------------------------------------------*/
/* This is the guts of the code to get a file via http.
url is the input url
httpfile is set to be the file connected to the socket which you can
read the file from
contentencoding is the mime type of the file, returned if the http server
returns it
contentlength is the length of the file, returned if the http server returns
it
*/
static int http_open_network(char *url, FILE **httpfile, char *contentencoding,
int *contentlength)
{
int status;
int sock;
int tmpint;
char recbuf[MAXLEN];
char tmpstr[MAXLEN];
char tmpstr1[SHORTLEN];
char tmpstr2[MAXLEN];
char errorstr[MAXLEN];
char proto[SHORTLEN];
char host[SHORTLEN];
char userpass[MAXLEN];
char fn[MAXLEN];
char turl[MAXLEN];
char *scratchstr;
char *scratchstr2;
char *saveptr;
int port;
float version;
char pproto[SHORTLEN];
char phost[SHORTLEN]; /* address of the proxy server */
int pport; /* port number of the proxy server */
char pfn[MAXLEN];
char *proxy; /* URL of the proxy server */
/* Parse the URL apart again */
strcpy(turl,"http://");
strncat(turl,url,MAXLEN - 8);
if (NET_ParseUrl(turl,proto,host,&port,fn)) {
snprintf(errorstr,MAXLEN,"URL Parse Error (http_open) %s",url);
ffpmsg(errorstr);
return (FILE_NOT_OPENED);
}
/* Do we have a user:password combo ? */
strcpy(userpass, url);
if ((scratchstr = strchr(userpass, '@')) != NULL) {
*scratchstr = '\0';
} else {
strcpy(userpass, "");
}
/* Ph. Prugniel 2003/04/03
Are we using a proxy?
We use a proxy if the environment variable "http_proxy" is set to an
address, eg. http://wwwcache.nottingham.ac.uk:3128
("http_proxy" is also used by wget)
*/
proxy = getenv("http_proxy");
/* Connect to the remote host */
if (proxy) {
if (NET_ParseUrl(proxy,pproto,phost,&pport,pfn)) {
snprintf(errorstr,MAXLEN,"URL Parse Error (http_open) %s",proxy);
ffpmsg(errorstr);
return (FILE_NOT_OPENED);
}
sock = NET_TcpConnect(phost,pport);
} else {
sock = NET_TcpConnect(host,port);
}
if (sock < 0) {
if (proxy) {
ffpmsg("Couldn't connect to host via proxy server (http_open_network)");
ffpmsg(proxy);
}
return (FILE_NOT_OPENED);
}
/* Make the socket a stdio file */
if (NULL == (*httpfile = fdopen(sock,"r"))) {
ffpmsg ("fdopen failed to convert socket to file (http_open_network)");
close(sock);
return (FILE_NOT_OPENED);
}
/* Send the GET request to the remote server */
/* Ph. Prugniel 2003/04/03
One must add the Host: command because of HTTP 1.1 servers (ie. virtual
hosts) */
if (proxy) {
snprintf(tmpstr,MAXLEN,"GET http://%s:%-d%s HTTP/1.0\r\n",host,port,fn);
} else {
snprintf(tmpstr,MAXLEN,"GET %s HTTP/1.0\r\n",fn);
}
if (strcmp(userpass, "")) {
encode64(strlen(userpass), userpass, MAXLEN, tmpstr2);
snprintf(tmpstr1, SHORTLEN,"Authorization: Basic %s\r\n", tmpstr2);
if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1)
{
fclose(*httpfile);
*httpfile=0;
return (FILE_NOT_OPENED);
}
strcat(tmpstr,tmpstr1);
}
/* snprintf(tmpstr1,SHORTLEN,"User-Agent: HEASARC/CFITSIO/%-8.3f\r\n",ffvers(&version)); */
/* snprintf(tmpstr1,SHORTLEN,"User-Agent: CFITSIO/HEASARC/%-8.3f\r\n",ffvers(&version)); */
snprintf(tmpstr1,SHORTLEN,"User-Agent: FITSIO/HEASARC/%-8.3f\r\n",ffvers(&version));
if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1)
{
fclose(*httpfile);
*httpfile=0;
return (FILE_NOT_OPENED);
}
strcat(tmpstr,tmpstr1);
/* HTTP 1.1 servers require the following 'Host: ' string */
snprintf(tmpstr1,SHORTLEN,"Host: %s:%-d\r\n\r\n",host,port);
if (strlen(tmpstr) + strlen(tmpstr1) > MAXLEN - 1)
{
fclose(*httpfile);
*httpfile=0;
return (FILE_NOT_OPENED);
}
strcat(tmpstr,tmpstr1);
status = NET_SendRaw(sock,tmpstr,strlen(tmpstr),NET_DEFAULT);
/* read the header */
if (!(fgets(recbuf,MAXLEN,*httpfile))) {
snprintf (errorstr,MAXLEN,"http header short (http_open_network) %s",recbuf);
ffpmsg(errorstr);
fclose(*httpfile);
*httpfile=0;
return (FILE_NOT_OPENED);
}
*contentlength = 0;
contentencoding[0] = '\0';
/* Our choices are 200, ok, 302, temporary redirect, or 301 perm redirect */
sscanf(recbuf,"%s %d",tmpstr,&status);
if (status != 200){
if (status == 301 || status == 302) {
/* got a redirect */
/*
if (status == 302) {
ffpmsg("Note: Web server replied with a temporary redirect from");
} else {
ffpmsg("Note: Web server replied with a redirect from");
}
ffpmsg(turl);
*/
/* now, let's not write the most sophisticated parser here */
while (fgets(recbuf,MAXLEN,*httpfile)) {
scratchstr = strstr(recbuf,"Location: ");
if (scratchstr != NULL) {
/* Ok, we found the Location line which gives the redirected URL */
/* skip the "Location: " charactrers */
scratchstr += 10;
/* strip off any end-of-line characters */
tmpint = strlen(scratchstr);
if (scratchstr[tmpint-1] == '\r') scratchstr[tmpint-1] = '\0';
tmpint = strlen(scratchstr);
if (scratchstr[tmpint-1] == '\n') scratchstr[tmpint-1] = '\0';
tmpint = strlen(scratchstr);
if (scratchstr[tmpint-1] == '\r') scratchstr[tmpint-1] = '\0';
/*
ffpmsg("to:");
ffpmsg(scratchstr);
ffpmsg(" ");
*/
scratchstr2 = strstr(scratchstr,"http://");
if (scratchstr2 != NULL) {
/* Ok, we found the HTTP redirection is to another HTTP URL. */
/* We can handle this case directly, here */
/* skip the "http://" characters */
scratchstr2 += 7;
strcpy(turl, scratchstr2);
fclose (*httpfile);
*httpfile=0;
/* note the recursive call to itself */
return
http_open_network(turl,httpfile,contentencoding,contentlength);
}
/* It was not a HTTP to HTTP redirection, so see if it HTTP to FTP */
scratchstr2 = strstr(scratchstr,"ftp://");
if (scratchstr2 != NULL) {
/* Ok, we found the HTTP redirection is to a FTP URL. */
/* skip the "ftp://" characters */
scratchstr2 += 6;
/* return the new URL string, and set contentencoding to "ftp" as
a flag to the http_checkfile routine
*/
if (strlen(scratchstr2) > FLEN_FILENAME-1)
{
ffpmsg("Error: redirected url string too long (http_open_network)");
fclose(*httpfile);
*httpfile=0;
return URL_PARSE_ERROR;
}
strcpy(url, scratchstr2);
strcpy(contentencoding,"ftp://");
fclose (*httpfile);
*httpfile=0;
return 0;
}
/* Now check for HTTP to HTTPS redirection. */
scratchstr2 = strstr(scratchstr,"https://");
if (scratchstr2 != NULL) {
/* skip the "https://" characters */
scratchstr2 += 8;
/* return the new URL string, and set contentencoding to "https" as
a flag to the http_checkfile routine
*/
if (strlen(scratchstr2) > FLEN_FILENAME-1)
{
ffpmsg("Error: redirected url string too long (http_open_network)");
fclose(*httpfile);
return URL_PARSE_ERROR;
}
strcpy(url, scratchstr2);
strcpy(contentencoding,"https://");
fclose(*httpfile);
*httpfile=0;
return 0;
}
}
}
/* if we get here then we couldnt' decide the redirect */
ffpmsg("but we were unable to find the redirected url in the servers response");
}
/* error. could not open the http file */
fclose(*httpfile);
*httpfile=0;
return (FILE_NOT_OPENED);