-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-client.c
1025 lines (805 loc) · 22.4 KB
/
http-client.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
#include "main.h"
#define BUF_LEN 65535 /* バッファのサイズ */
#define BUFFSIZE 65535
void check_msg_from_parent();
static gint fd_check_io();
gint fd_recv();
gint fd_gets();
char *read_line();
void read_response();
gint fd_write();
void write_to_server();
#ifdef USE_SSL
void write_to_SSLserver();
#endif
void error();
void PortReq();
int sftp_c();
int sftp_get_c();
int ftp_c();
int http_c_nonssl();
#ifdef USE_SSL
int http_c_ssl();
#endif
void unchunk();
#ifdef USE_SSL
gint ssl_gets();
gint ssl_read();
gint ssl_peek();
gint ssl_write();
#endif
gboolean progress_timeout();
void dl_camz_list();
glong get_file_size();
void write_dlsz();
void unlink_dlsz();
glong get_dlsz();
static void cancel_http();
static void thread_cancel_http();
void read_camz();
#ifdef POP_DEBUG
gboolean debug_flg=TRUE;
#else
gboolean debug_flg=FALSE;
#endif
void check_msg_from_parent(){
}
static gint fd_check_io(gint fd, GIOCondition cond)
{
struct timeval timeout;
fd_set fds;
guint io_timeout=60;
timeout.tv_sec = io_timeout;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (cond == G_IO_IN) {
select(fd + 1, &fds, NULL, NULL,
io_timeout > 0 ? &timeout : NULL);
} else {
select(fd + 1, NULL, &fds, NULL,
io_timeout > 0 ? &timeout : NULL);
}
if (FD_ISSET(fd, &fds)) {
return 0;
} else {
g_warning("Socket IO timeout\n");
return -1;
}
}
gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
{
gint ret;
if (fd_check_io(fd, G_IO_IN) < 0)
return -1;
ret = recv(fd, buf, len, flags);
return ret;
}
gint fd_gets(gint fd, gchar *buf, gint len)
{
gchar *newline, *bp = buf;
gint n;
if (--len < 1)
return -1;
do {
if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
return -1;
if ((newline = memchr(bp, '\n', n)) != NULL)
n = newline - bp + 1;
if ((n = fd_recv(fd, bp, n, 0)) < 0)
return -1;
bp += n;
len -= n;
} while (!newline && len);
*bp = '\0';
return bp - buf;
}
/*--------------------------------------------------
* ソケットから1行読み込む
*/
char *read_line(int socket, char *p){
char *org_p = p;
while (1){
if ( read(socket, p, 1) == 0 ) break;
if ( *p == '\n' ) break;
p++;
}
*(++p) = '\0';
return org_p;
}
/*--------------------------------------------------
* レスポンスを取得する。^\d\d\d- ならもう1行取得
*/
void read_response(int socket, char *p){
int ret;
do {
//read_line(socket, p);
ret=fd_gets(socket,p,BUF_LEN);
if ( debug_flg ){
fprintf(stderr, "<-- %s", p);fflush(stderr);
}
} while ( isdigit(p[0]) &&
isdigit(p[1]) &&
isdigit(p[2]) &&
p[3]=='-' );
}
gint fd_write(gint fd, const gchar *buf, gint len)
{
if (fd_check_io(fd, G_IO_OUT) < 0)
return -1;
return write(fd, buf, len);
}
/*--------------------------------------------------
* 指定されたソケット socket に文字列 p を送信。
* 文字列 p の終端は \0 で terminate されている
* 必要がある
*/
void write_to_server(int socket, char *p){
if ( debug_flg ){
fprintf(stderr, "--> %s", p);fflush(stderr);
}
fd_write(socket, p, strlen(p));
}
#ifdef USE_SSL
void write_to_SSLserver(SSL *ssl, char *p){
if ( debug_flg ){
fprintf(stderr, "[SSL] <-- %s", p);fflush(stderr);
}
ssl_write(ssl, p, strlen(p));
}
#endif
void error( char *message ){
fprintf(stderr, "%s\n", message);
exit(1);
}
void PortReq(char *IPaddr , int *i1 , int *i2 , int *i3 , int *i4 , int *i5 , int *i6)
{
int j ;
char *ip ;
IPaddr = IPaddr + 3 ;
while( isdigit(*IPaddr) == 0 ) { IPaddr++ ; }
ip = strtok(IPaddr,",");
*i1 = atoi(ip) ;
ip = strtok(NULL,",");
*i2 = atoi(ip) ;
ip = strtok(NULL,",");
*i3 = atoi(ip) ;
ip = strtok(NULL,",");
*i4 = atoi(ip) ;
ip = strtok(NULL,",");
*i5 = atoi(ip) ;
ip = strtok(NULL,",");
j = 0 ;
while ( isdigit(*(ip +j)) != 0 ) { j += 1 ; }
ip[j] = '\0' ;
*i6 = atoi(ip) ;
}
gpointer thread_get_camz_list(gpointer gdata){
typHLOG *hl=(typHLOG *)gdata;
hl->pabort=FALSE;
//http_c_nonssl(hl);
http_c_ssl(hl);
if(hl->ploop) g_main_loop_quit(hl->ploop);
}
void unchunk(gchar *dss_tmp){
FILE *fp_read, *fp_write;
gchar *unchunk_tmp;
gchar cbuf[BUFFSIZE];
gchar *dbuf=NULL;
gchar *cpp;
gchar *chunkptr, *endptr;
long chunk_size;
gint i, read_size=0, crlf_size=0;
if ( debug_flg ){
fprintf(stderr, "Decoding chunked file \"%s\".\n", dss_tmp);fflush(stderr);
}
fp_read=fopen(dss_tmp,"r");
unchunk_tmp=g_strconcat(dss_tmp,"_unchunked",NULL);
fp_write=fopen(unchunk_tmp,"wb");
while(!feof(fp_read)){
if(fgets(cbuf,BUFFSIZE-1,fp_read)){
cpp=cbuf;
read_size=strlen(cpp);
for(i=read_size;i>=0;i--){
if(isalnum(cpp[i])){
crlf_size=read_size-i-1;
break;
}
else{
cpp[i]='\0';
}
}
chunkptr=g_strdup_printf("0x%s",cpp);
chunk_size=strtol(chunkptr, &endptr, 0);
g_free(chunkptr);
if(chunk_size==0) break;
if((dbuf = (gchar *)g_malloc(sizeof(gchar)*(chunk_size+crlf_size+1)))==NULL){
fprintf(stderr, "!!! Memory allocation error in unchunk() \"%s\".\n", dss_tmp);
fflush(stderr);
break;
}
if(fread(dbuf,1, chunk_size+crlf_size, fp_read)){
fwrite( dbuf , chunk_size , 1 , fp_write );
if(dbuf) g_free(dbuf);
}
else{
break;
}
}
}
fclose(fp_read);
fclose(fp_write);
unlink(dss_tmp);
rename(unchunk_tmp,dss_tmp);
g_free(unchunk_tmp);
}
#ifdef USE_SSL
gint ssl_gets(SSL *ssl, gchar *buf, gint len)
{
gchar *newline, *bp = buf;
gint n;
gint i;
if (--len < 1)
return -1;
do {
if ((n = ssl_peek(ssl, bp, len)) <= 0)
return -1;
if ((newline = memchr(bp, '\n', n)) != NULL)
n = newline - bp + 1;
if ((n = ssl_read(ssl, bp, n)) < 0)
return -1;
bp += n;
len -= n;
} while (!newline && len);
*bp = '\0';
return bp - buf;
}
#endif
#ifdef USE_SSL
gint ssl_read(SSL *ssl, gchar *buf, gint len)
{
gint err, ret;
if (SSL_pending(ssl) == 0) {
if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
return -1;
}
ret = SSL_read(ssl, buf, len);
switch ((err = SSL_get_error(ssl, ret))) {
case SSL_ERROR_NONE:
return ret;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
errno = EAGAIN;
return -1;
case SSL_ERROR_ZERO_RETURN:
return 0;
default:
g_warning("SSL_read() returned error %d, ret = %d\n", err, ret);
if (ret == 0)
return 0;
return -1;
}
}
#endif
/* peek at the socket data without actually reading it */
#ifdef USE_SSL
gint ssl_peek(SSL *ssl, gchar *buf, gint len)
{
gint err, ret;
if (SSL_pending(ssl) == 0) {
if (fd_check_io(SSL_get_rfd(ssl), G_IO_IN) < 0)
return -1;
}
ret = SSL_peek(ssl, buf, len);
switch ((err = SSL_get_error(ssl, ret))) {
case SSL_ERROR_NONE:
return ret;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
errno = EAGAIN;
return -1;
case SSL_ERROR_ZERO_RETURN:
return 0;
case SSL_ERROR_SYSCALL:
// End of file
//printf("SSL_ERROR_SYSCALL ret=%d %d\n",ret,(gint)strlen(buf));
return 0;
default:
g_warning("SSL_peek() returned error %d, ret = %d\n", err, ret);
if (ret == 0)
return 0;
return -1;
}
}
#endif
#ifdef USE_SSL
gint ssl_write(SSL *ssl, const gchar *buf, gint len)
{
gint ret;
ret = SSL_write(ssl, buf, len);
switch (SSL_get_error(ssl, ret)) {
case SSL_ERROR_NONE:
return ret;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
errno = EAGAIN;
return -1;
default:
return -1;
}
}
#endif
int http_c_nonssl(typHLOG *hl)
{
int command_socket; /* コマンド用ソケット */
int size;
char send_mesg[BUF_LEN]; /* サーバに送るメッセージ */
char buf[BUF_LEN+1];
FILE *fp_write;
FILE *fp_read;
struct addrinfo hints, *res;
struct in_addr addr;
int err;
gboolean chunked_flag=FALSE;
gchar *cp;
gchar *rand16=NULL;
gint plen;
check_msg_from_parent();
/* ホストの情報 (IP アドレスなど) を取得 */
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if ((err = getaddrinfo(hl->http_host, "http", &hints, &res)) !=0){
fprintf(stderr, "Bad hostname [%s]\n", hl->http_host);
return(HDSLOG_HTTP_ERROR_GETHOST);
}
check_msg_from_parent();
/* ソケット生成 */
if( (command_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
fprintf(stderr, "Failed to create a new socket.\n");
return(HDSLOG_HTTP_ERROR_SOCKET);
}
check_msg_from_parent();
/* サーバに接続 */
if( connect(command_socket, res->ai_addr, res->ai_addrlen) == -1){
fprintf(stderr, "Failed to connect to %s .\n", hl->http_host);
return(HDSLOG_HTTP_ERROR_CONNECT);
}
check_msg_from_parent();
// AddrInfoの解放
freeaddrinfo(res);
// HTTP/1.1 ではchunked対策が必要
sprintf(send_mesg, "GET %s HTTP/1.1\r\n", hl->http_path);
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "Accept: text/plain,text/html,application/x-gzip\r\n");
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "Accept-Encoding: gzip\r\n");
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "User-Agent: Mozilla/5.0\r\n");
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "Host: %s\r\n", hl->http_host);
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "Connection: close\r\n");
write_to_server(command_socket, send_mesg);
sprintf(send_mesg, "\r\n");
write_to_server(command_socket, send_mesg);
if((fp_write=fopen(hl->http_dlfile,"wb"))==NULL){
fprintf(stderr," File Write Error \"%s\" \n", hl->http_dlfile);
return(HDSLOG_HTTP_ERROR_TEMPFILE);
}
unlink_dlsz(hl);
while((size = fd_gets(command_socket,buf,BUF_LEN)) > 2 ){
// header lines
if(debug_flg){
fprintf(stderr,"--> Header: %s", buf);
}
if(NULL != (cp = strstr(buf, "Transfer-Encoding: chunked"))){
chunked_flag=TRUE;
}
if(strncmp(buf,"Content-Length: ",strlen("Content-Length: "))==0){
cp = buf + strlen("Content-Length: ");
hl->http_dlsz=atol(cp);
}
}
write_dlsz(hl);
do{ // data read
size = recv(command_socket,buf,BUF_LEN, 0);
fwrite( &buf , size , 1 , fp_write );
}while(size>0);
fclose(fp_write);
check_msg_from_parent();
if(chunked_flag) unchunk(hl->http_dlfile);
if((chmod(hl->http_dlfile,(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH ))) != 0){
g_print("Cannot Chmod Temporary File %s! Please check!!!\n",hl->http_dlfile);
}
unlink_dlsz(hl);
close(command_socket);
return 0;
}
#ifdef USE_SSL
int http_c_ssl(typHLOG *hl)
{
int command_socket; /* コマンド用ソケット */
int size;
char send_mesg[BUF_LEN]; /* サーバに送るメッセージ */
char buf[BUF_LEN+1];
FILE *fp_write;
FILE *fp_read;
struct addrinfo hints, *res;
struct addrinfo dhints, *dres;
struct in_addr addr;
int err, ret;
gboolean chunked_flag=FALSE;
gchar *cp;
gchar *rand16=NULL;
gint plen;
SSL *ssl;
SSL_CTX *ctx;
check_msg_from_parent();
/* ホストの情報 (IP アドレスなど) を取得 */
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if ((err = getaddrinfo(hl->http_host, "https", &hints, &res)) !=0){
fprintf(stderr, "Bad hostname [%s]\n", hl->http_host);
return(HDSLOG_HTTP_ERROR_GETHOST);
}
check_msg_from_parent();
/* ソケット生成 */
if( (command_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
fprintf(stderr, "Failed to create a new socket.\n");
return(HDSLOG_HTTP_ERROR_SOCKET);
}
check_msg_from_parent();
/* サーバに接続 */
if( connect(command_socket, res->ai_addr, res->ai_addrlen) == -1){
fprintf(stderr, "Failed to connect to %s .\n", hl->http_host);
return(HDSLOG_HTTP_ERROR_CONNECT);
}
check_msg_from_parent();
SSL_load_error_strings();
SSL_library_init();
ctx = SSL_CTX_new(SSLv23_client_method());
ssl = SSL_new(ctx);
err = SSL_set_fd(ssl, command_socket);
while((ret=SSL_connect(ssl))!=1){
err=SSL_get_error(ssl, ret);
if( (err==SSL_ERROR_WANT_READ)||(err==SSL_ERROR_WANT_WRITE) ){
g_usleep(100000);
g_warning("SSL_connect(): try again\n");
continue;
}
g_warning("SSL_connect() failed with error %d, ret=%d (%s)\n",
err, ret, ERR_error_string(ERR_get_error(), NULL));
return (HDSLOG_HTTP_ERROR_CONNECT);
}
check_msg_from_parent();
// AddrInfoの解放
freeaddrinfo(res);
// HTTP/1.1 ではchunked対策が必要
sprintf(send_mesg, "GET %s HTTP/1.1\r\n", hl->http_path);
write_to_SSLserver(ssl, send_mesg);
sprintf(send_mesg, "Accept: application/xml, application/json\r\n");
write_to_SSLserver(ssl, send_mesg);
sprintf(send_mesg, "User-Agent: Mozilla/5.0\r\n");
write_to_SSLserver(ssl, send_mesg);
sprintf(send_mesg, "Host: %s\r\n", hl->http_host);
write_to_SSLserver(ssl, send_mesg);
sprintf(send_mesg, "Connection: close\r\n");
write_to_SSLserver(ssl, send_mesg);
sprintf(send_mesg, "\r\n");
write_to_SSLserver(ssl, send_mesg);
if((fp_write=fopen(hl->http_dlfile,"wb"))==NULL){
fprintf(stderr," File Write Error \"%s\" \n", hl->http_dlfile);
return(HDSLOG_HTTP_ERROR_TEMPFILE);
}
while((size = ssl_gets(ssl, buf, BUF_LEN)) > 2 ){
// header lines
if(debug_flg){
fprintf(stderr,"[SSL] --> Header: %s", buf);
}
if(NULL != (cp = strstr(buf, "Transfer-Encoding: chunked"))){
chunked_flag=TRUE;
}
}
do{ // data read
size = SSL_read(ssl, buf, BUF_LEN);
fwrite( &buf , size , 1 , fp_write );
}while(size >0);
fclose(fp_write);
check_msg_from_parent();
if(chunked_flag) unchunk(hl->http_dlfile);
if((chmod(hl->http_dlfile,(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |S_IROTH | S_IWOTH ))) != 0){
g_print("Cannot Chmod Temporary File %s! Please check!!!\n",hl->http_dlfile);
}
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
ERR_free_strings();
close(command_socket);
return 0;
}
#endif //USE_SSL
gboolean progress_timeout( gpointer data ){
typHLOG *hl=(typHLOG *)data;
glong sz=-1;
gchar *tmp;
gdouble frac;
if(!hl->http_ok){
return(FALSE);
}
if(gtk_widget_get_realized(hl->pbar)){
sz=get_file_size(hl->http_dlfile);
if(sz>0){ // After Downloading Started to get current dlsz
if(hl->http_dlsz<0){
hl->http_dlsz=get_dlsz(hl);
}
}
if(hl->http_dlsz>0){
frac=(gdouble)sz/(gdouble)hl->http_dlsz;
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(hl->pbar),
frac);
if(sz>1024*1024){
tmp=g_strdup_printf("%d%% Downloaded (%.2lf / %.2lf MB)",
(gint)(frac*100.),
(gdouble)sz/1024./1024.,
(gdouble)hl->http_dlsz/1024./1024.);
}
else if(sz>1024){
tmp=g_strdup_printf("%d%% Downloaded (%ld / %ld kB)",
(gint)(frac*100.),
sz/1024,
hl->http_dlsz/1024);
}
else{
tmp=g_strdup_printf("%d%% Downloaded (%ld / %ld bytes)",
(gint)(frac*100.),
sz, hl->http_dlsz);
}
}
else{
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(hl->pbar));
if(sz>1024*1024){
tmp=g_strdup_printf("Downloaded %.2lf MB",
(gdouble)sz/1024./1024.);
}
else if(sz>1024){
tmp=g_strdup_printf("Downloaded %ld kB", sz/1024);
}
else if (sz>0){
tmp=g_strdup_printf("Downloaded %ld bytes", sz);
}
else{
tmp=g_strdup_printf("Waiting for HTTP server response ...");
}
}
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(hl->pbar),
tmp);
g_free(tmp);
}
return TRUE;
}
void popup_dl_camz_list(GtkWidget *w, gpointer gdata){
typHLOG *hl = (typHLOG *)gdata;
dl_camz_list(hl, TRUE);
}
void dl_camz_list(typHLOG *hl, gboolean flag_popup){
GtkWidget *vbox, *label, *button, *bar, *hbox;
gint timer=-1;
gchar *tmp;
hl->http_ok=TRUE;
if(hl->http_host) g_free(hl->http_host);
hl->http_host=g_strdup(HTTP_CAMZ_HOST);
if(hl->http_path) g_free(hl->http_path);
hl->http_path=g_strdup(HTTP_CAMZ_PATH);
if(hl->http_dlfile) g_free(hl->http_dlfile);
hl->http_dlfile=g_strdup_printf("%s%s%s-%d",
g_get_tmp_dir(), G_DIR_SEPARATOR_S,
HTTP_CAMZ_FILE, getuid());
if(access(hl->http_dlfile, F_OK)==0) unlink(hl->http_dlfile);
hl->pdialog = gtk_dialog_new();
gtk_window_set_position(GTK_WINDOW(hl->pdialog), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(hl->pdialog),5);
gtk_container_set_border_width(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),5);
gtk_window_set_title(GTK_WINDOW(hl->pdialog),"Subaru HDS LOG : Downloading Latest CamZ Values");
gtk_window_set_decorated(GTK_WINDOW(hl->pdialog),TRUE);
label=gtkut_label_new("Downloading the latest CamZ ...");
#ifdef USE_GTK3
gtk_widget_set_halign (label, GTK_ALIGN_START);
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
#else
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
#endif
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),
label,TRUE,TRUE,0);
gtk_widget_show(label);
hl->http_dlsz=-1;
hl->pbar=gtk_progress_bar_new();
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),
hl->pbar,TRUE,TRUE,0);
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(hl->pbar));
#ifdef USE_GTK3
gtk_orientable_set_orientation (GTK_ORIENTABLE (hl->pbar),
GTK_ORIENTATION_HORIZONTAL);
css_change_pbar_height(hl->pbar,15);
gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(hl->pbar), TRUE);
#else
gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (hl->pbar),
GTK_PROGRESS_RIGHT_TO_LEFT);
#endif
gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(hl->pbar),0.05);
gtk_widget_show(hl->pbar);
#ifdef USE_GTK3
bar = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
#else
bar = gtk_hseparator_new();
#endif
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),
bar,FALSE, FALSE, 0);
label=gtkut_label_new("Checking the latest CamZ values ...");
#ifdef USE_GTK3
gtk_widget_set_halign (label, GTK_ALIGN_START);
gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
#else
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
#endif
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),
label,FALSE,FALSE,0);
hbox = gtkut_hbox_new (FALSE, 0);
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(hl->pdialog))),
hbox,TRUE, TRUE, 0);
#ifdef USE_GTK3
button=gtkut_button_new_from_icon_name("Cancel","process-stop");
#else
button=gtkut_button_new_from_stock("Cancel",GTK_STOCK_CANCEL);
#endif
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE,0),
g_signal_connect(button,"pressed",
G_CALLBACK(thread_cancel_http),
(gpointer)hl);
gtk_widget_show_all(hl->pdialog);
timer=g_timeout_add(100,
(GSourceFunc)progress_timeout,
(gpointer)hl);
gtk_window_set_modal(GTK_WINDOW(hl->pdialog),TRUE);
hl->ploop=g_main_loop_new(NULL, FALSE);
hl->pcancel=g_cancellable_new();
hl->pthread=g_thread_new("hdslog_get_camz_list", thread_get_camz_list, (gpointer)hl);
g_main_loop_run(hl->ploop);
g_thread_join(hl->pthread);
g_main_loop_unref(hl->ploop);
hl->ploop=NULL;
//get_camz_list(hl);
//gtk_main();
gtk_window_set_modal(GTK_WINDOW(hl->pdialog),FALSE);
if(timer!=-1) g_source_remove(timer);
if(GTK_IS_WIDGET(hl->pdialog)) gtk_widget_destroy(hl->pdialog);
if(access(hl->http_dlfile, F_OK)==0){
read_camz(hl);
}
else{
tmp=g_strconcat(hl->http_host,
hl->http_path,
NULL);
fprintf(stderr, "Error : Failed to download CamZ file \"%s\"\n", tmp);
g_free(tmp);
}
}
glong get_file_size(gchar *fname)
{
FILE *fp;
long sz;
fp = fopen( fname, "rb" );
if( fp == NULL ){
return -1;
}
fseek( fp, 0, SEEK_END );
sz = ftell( fp );
fclose( fp );
return sz;
}
void write_dlsz(typHLOG *hl){
FILE *fp;
gchar *tmp_file;
tmp_file=g_strdup_printf("%s%s%s-%d",
g_get_tmp_dir(), G_DIR_SEPARATOR_S,
HTTP_DLSZ_FILE, getuid());
if((fp=fopen(tmp_file,"w"))==NULL){
fprintf(stderr," File Write Error \"%s\" \n", tmp_file);
g_free(tmp_file);
return;
}
fprintf(fp, "%ld\n",hl->http_dlsz);
fclose(fp);
g_free(tmp_file);
return;
}
void unlink_dlsz(typHLOG *hl){
gchar *tmp_file;
hl->http_dlsz=0;
tmp_file=g_strdup_printf("%s%s%s-%d",
g_get_tmp_dir(), G_DIR_SEPARATOR_S,
HTTP_DLSZ_FILE, getuid());
if(access(tmp_file, F_OK)==0){
unlink(tmp_file);
}
g_free(tmp_file);
return;
}
glong get_dlsz(typHLOG *hl){
FILE *fp;
gchar *tmp_file;
glong sz=0;
gchar buf[10];
tmp_file=g_strdup_printf("%s%s%s-%d",
g_get_tmp_dir(), G_DIR_SEPARATOR_S,
HTTP_DLSZ_FILE, getuid());
if((fp=fopen(tmp_file,"r"))==NULL){
g_free(tmp_file);
return(-1);
}
if(fgets(buf,10-1,fp)){
sz = atol(buf);
}
fclose(fp);
unlink(tmp_file);
g_free(tmp_file);
return (sz);
}
static void thread_cancel_http(GtkWidget *w, gpointer gdata)
{
typHLOG *hl=(typHLOG *)gdata;
if(GTK_IS_WIDGET(hl->pdialog)) gtk_widget_unmap(hl->pdialog);
g_cancellable_cancel(hl->pcancel);
g_object_unref(hl->pcancel);
hl->pabort=TRUE;
unlink_dlsz(hl);
if(access(hl->http_dlfile, F_OK)==0) unlink(hl->http_dlfile);
if(hl->ploop) g_main_loop_quit(hl->ploop);
}
void read_camz(typHLOG *hl){
FILE *fp;
gchar *buf=NULL, *cp, *cpp, *tmp_char=NULL, *head=NULL, *tmp_p;
gchar *tmp;
gint i;
if((fp=fopen(hl->http_dlfile,"rb"))==NULL){
fprintf(stderr, "Error: Cannot open \"%s\".\n",hl->http_dlfile);
return;
}
while((buf=fgets_new(fp))!=NULL){
tmp_char=(char *)strtok(buf,",");
if(strncmp(tmp_char,"CamZ_B",strlen("CamZ_B"))==0){
if((tmp_p=strtok(NULL,","))!=NULL){
hl->camz_b=g_strtod(tmp_p,NULL);
}
}
if(strncmp(tmp_char,"CamZ_R",strlen("CamZ_R"))==0){
if((tmp_p=strtok(NULL,","))!=NULL){
hl->camz_r=g_strtod(tmp_p,NULL);
}
}
if(strncmp(tmp_char,"RdCross",strlen("RdCross"))==0){