~ubuntu-branches/ubuntu/lucid/edbrowse/lucid

« back to all changes in this revision

Viewing changes to http.c

  • Committer: Bazaar Package Importer
  • Author(s): Kapil Hari Paranjape
  • Date: 2007-12-05 10:39:04 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20071205103904-zr3kzzu0ym6stg16
Tags: 3.3.1-1
* New upstream version (3.3.1).
  - Messages have been internationalised.
  - Supports LANG environment variable.
  - Includes man page.
* Incorporate upstream man page:
  - debian/rules: Add rule to install upstream man page.
  - debian/edbrowse.1: Not required. Removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
    if(!sslCerts) {
60
60
        verifyCertificates = false;
61
61
        if(doConfig)
62
 
            debugPrint(1,
63
 
               "no ssl certificate file specified; secure connections cannot be verified");
 
62
            if(debugLevel >= 1)
 
63
                i_puts(MSG_NoCertFile);
64
64
    }
65
65
 
66
66
    SSL_CTX_set_default_verify_paths(sslcx);
115
115
        else
116
116
            n = tcp_readFully(fh, p->data, CHUNKSIZE);
117
117
        if(n < 0) {
118
 
            setError(intFlag ? opint :
119
 
               "could not read the data from the server");
 
118
            setError(intFlag ? MSG_Interrupted : MSG_WebRead);
120
119
            free(p);
121
120
            for(p = chunklist; p; p = q) {
122
121
                q = p->next;
140
139
        isprintByte = true;
141
140
    }                           /* loop reading data */
142
141
    if(isprintByte)
143
 
        printf("\n");
 
142
        nl();
144
143
 
145
144
/* Put it all together */
146
145
    serverData = data = allocMem(len + 1);
220
219
time_t
221
220
parseHeaderDate(const char *date)
222
221
{
223
 
    static const char *const months[12] =
224
 
       { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
 
222
    static const char *const months[12] = {
 
223
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
225
224
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
226
225
    };
227
226
    time_t t = 0;
401
400
        *delay_p = delay;
402
401
        return true;
403
402
    }
404
 
    debugPrint(0, "warning: garbled refresh directive, %s", ref);
 
403
    i_printf(MSG_GarbledRefresh, ref);
405
404
    *delay_p = 0;
406
405
    return false;
407
406
}                               /* parseRefresh */
414
413
/* the value 15 seconds is somewhat arbitrary */
415
414
    if(sec < 15)
416
415
        return true;
417
 
    printf("redirect to %s delayed by %d seconds", u, sec);
 
416
    i_printf(MSG_RedirectDelayed, u, sec);
418
417
    return false;
419
418
}                               /* refreshDelay */
420
419
 
446
445
    char suffix[12];
447
446
 
448
447
    if(!isURL(url)) {
449
 
        setError("%s is not a url", url);
 
448
        setError(MSG_BadURL, url);
450
449
        return false;
451
450
    }
452
451
 
470
469
    secure = stringEqualCI(prot, "https");
471
470
    host = getHostURL(url);
472
471
    if(!host)
473
 
        errorPrint("@empty host in httpConnect");
 
472
        i_printfExit(MSG_EmptyHost);
474
473
    if(proxy_host) {
475
474
        if(secure) {
476
 
            setError("secure proxy not yet implemented");
 
475
            setError(MSG_SSLProxyNYI);
477
476
            return false;
478
477
        }
479
478
        hip = tcp_name_ip(proxy_host);
481
480
        hip = tcp_name_ip(host);
482
481
    }
483
482
    if(hip == -1) {
484
 
        setError(intFlag ? opint : "cannot identify %s on the network", host);
 
483
        setError((intFlag ? MSG_Interrupted : MSG_IdentifyHost), host);
485
484
        return false;
486
485
    }
487
486
    debugPrint(4, "%s -> %s",
499
498
        nzFree(cmd);
500
499
        return true;
501
500
    } else {
502
 
        setError
503
 
           ("the %s protocol is not supported by edbrowse, and is not included in the mime types in your config file",
504
 
           prot);
 
501
        setError(MSG_WebProtBad, prot);
505
502
        return false;
506
503
    }
507
504
 
524
521
        s = getUserURL(url);
525
522
        if(s) {
526
523
            if(strlen(s) >= sizeof (user) - 2) {
527
 
                setError("user name too long, limit %d characters",
528
 
                   sizeof (user));
 
524
                setError(MSG_UserNameLong, sizeof (user));
529
525
                return false;
530
526
            }
531
527
            strcpy(user, s);
533
529
        s = getPassURL(url);
534
530
        if(s) {
535
531
            if(strlen(s) >= sizeof (pass) - 2) {
536
 
                setError("password too long, limit %d characters",
537
 
                   sizeof (pass));
 
532
                setError(MSG_PasswordLong, sizeof (pass));
538
533
                return false;
539
534
            }
540
535
            strcpy(pass, s);
550
545
    getPortLocURL(url, &portloc, &port);
551
546
    hsock = tcp_connect(hip, (proxy_host ? proxy_port : port), webTimeout);
552
547
    if(hsock < 0) {
553
 
        setError(intFlag ? opint : "cannot connect to %s", host);
 
548
        setError((intFlag ? MSG_Interrupted : MSG_WebConnect), host);
554
549
        return false;
555
550
    }
556
551
    if(proxy_host)
568
563
            err = ERR_peek_last_error();
569
564
            ERR_clear_error();
570
565
            if(ERR_GET_REASON(err) != SSL_R_CERTIFICATE_VERIFY_FAILED)
571
 
                setError("cannot establish a secure connection to %s, error %d",
572
 
                   host, err);
 
566
                setError(MSG_WebConnectSecure, host, err);
573
567
            else
574
 
                setError
575
 
                   ("The certificate for host %s could not be verified - SSL connection aborted",
576
 
                   host);
 
568
                setError(MSG_NoCertify, host);
577
569
            SSL_free(hssl);
578
570
            close(hsock);
579
571
            return false;
673
665
            post += 5;
674
666
            stringAndString(&hdr, &l,
675
667
               "Content-Type: multipart/form-data; boundary=");
676
 
            s = strchr(post, '\n');
 
668
            s = strchr(post, '\r');
677
669
            stringAndBytes(&hdr, &l, post, s - post);
678
 
            post = s + 1;
 
670
            post = s + 2;
679
671
            stringAndString(&hdr, &l, eol);
680
672
        }
681
673
        stringAndString(&hdr, &l, "Content-Length: ");
707
699
    debugPrint(4, "http header sent, %d/%d bytes", n, l);
708
700
    free(hdr);
709
701
    if(n < l) {
710
 
        setError(intFlag ? opint :
711
 
           "could not send the request to the web server");
 
702
        setError(intFlag ? MSG_Interrupted : MSG_WebSend);
712
703
        if(secure)
713
704
            SSL_free(hssl);
714
705
        close(hsock);
801
792
        int authmeth = 1;       /* basic method by default */
802
793
        if(u = extractHeaderItem(serverData, hdr, "WWW-Authenticate", 0)) {
803
794
            if(!memEqualCI(u, "basic", 5) || isalnumByte(u[5])) {
804
 
                setError("authorization method %s not recognized", u);
 
795
                setError(MSG_BadAuthMethod, u);
805
796
                nzFree(u);
806
797
                goto abort;
807
798
            }
824
815
        }
825
816
        if(!(user[0] | pass[0])) {
826
817
            if(!isInteractive) {
827
 
                setError("web page requires authorization");
 
818
                setError(MSG_Authorize2);
828
819
                goto abort;
829
820
            }
830
 
            puts("authorization requested - type x to abort.");
 
821
            i_puts(MSG_WebAuthorize);
831
822
          getlogin:
832
 
            printf("Username: ");
 
823
            i_printf(MSG_UserName);
833
824
            fflush(stdout);
834
825
            fflush(stdin);
835
826
            if(!fgets(user, sizeof (user), stdin))
836
827
                ebClose(0);
837
828
            n = strlen(user);
838
829
            if(n >= sizeof (user) - 1) {
839
 
                printf("user name too long, limit %d characters",
840
 
                   sizeof (user) - 2);
 
830
                i_printf(MSG_UserNameLong, sizeof (user) - 2);
841
831
                goto getlogin;
842
832
            }
843
833
            if(n && user[n - 1] == '\n')
844
834
                user[--n] = 0;
845
835
            if(stringEqual(user, "x")) {
846
 
                setError("login aborted");
 
836
                setError(MSG_LoginAbort);
847
837
                goto abort;
848
838
            }
849
 
            printf("Password: ");
 
839
            i_printf(MSG_Password);
850
840
            fflush(stdout);
851
841
            fflush(stdin);
852
842
            if(!fgets(pass, sizeof (pass), stdin))
853
843
                ebClose(0);
854
844
            n = strlen(pass);
855
845
            if(n >= sizeof (pass) - 1) {
856
 
                printf("user name too long, limit %d characters",
857
 
                   sizeof (pass) - 2);
 
846
                i_printf(MSG_PasswordLong, sizeof (pass) - 2);
858
847
                goto getlogin;
859
848
            }
860
849
            if(n && pass[n - 1] == '\n')
861
850
                pass[--n] = 0;
862
851
            if(stringEqual(pass, "x")) {
863
 
                setError("login aborted");
 
852
                setError(MSG_LoginAbort);
864
853
                goto abort;
865
854
            }
866
855
        }
884
873
        if(stringEqualCI(u, "8bit"))
885
874
            hce = 0;
886
875
        if(hce < 0) {
887
 
            debugPrint(0, "warning: unrecognized http compression method %s",
888
 
               u);
 
876
            i_printf(MSG_CompressUnrecognized, u);
889
877
            hce = 0;
890
878
        }
891
879
        nzFree(u);
920
908
            if(!loc[0])
921
909
                loc = 0;
922
910
            if(!loc) {
923
 
                debugPrint(0,
924
 
                   "warning: http redirection %d, but a new url is not specified",
925
 
                   hcode);
 
911
                i_printf(MSG_RedirectNoURL, hcode);
926
912
                if(hcode >= 301 && hcode <= 303)
927
913
                    hcode = 200;
928
914
            } else
943
929
                }
944
930
                if(recount >= 10) {
945
931
                    free(u);
946
 
                    puts("too many redirections");
 
932
                    i_puts(MSG_RedirectMany);
947
933
                    goto gotFile;
948
934
                }
949
935
/* Redirection looks valid, let's run with it. */
976
962
    }
977
963
    /* looking for redirection */
978
964
    if(hcode != 200)
979
 
        debugPrint(0, "warning, html error %d, %s", hcode, herror);
 
965
        i_printf(MSG_HTTPError, hcode, herror);
980
966
 
981
967
  gotFile:
982
968
/* Don't need the header any more */
1003
989
           open(edbrowseTempFile, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC,
1004
990
           0666);
1005
991
        if(fh < 0) {
1006
 
            setError("cannot create temp file %s, to uncompress the web page",
1007
 
               edbrowseTempFile);
 
992
            setError(MSG_TempNoCreate, edbrowseTempFile);
1008
993
            *u = 0;
1009
994
            goto abort;
1010
995
        }
1011
996
        if(write(fh, serverData, serverDataLen) < serverDataLen) {
1012
 
            setError("cannot write to temp file %s, to uncompress the web page",
1013
 
               edbrowseTempFile);
 
997
            setError(MSG_TempNoWrite, edbrowseTempFile);
1014
998
            close(fh);
1015
999
            *u = 0;
1016
1000
            goto abort;
1028
1012
        free(scmd);
1029
1013
        n = fileSizeByName(edbrowseTempFile);
1030
1014
        if(n <= 0) {
1031
 
            setError
1032
 
               ("zcat cannot uncompress the data, to reconstruct the web page");
 
1015
            setError(MSG_TempNoUncompress);
1033
1016
            return false;
1034
1017
        }
1035
1018
        serverData = allocMem(n + 2);
1036
1019
        fh = open(edbrowseTempFile, O_RDONLY | O_BINARY);
1037
1020
        if(fh < 0) {
1038
 
            setError("cannot access the uncompressed web page in %s",
1039
 
               edbrowseTempFile);
 
1021
            setError(MSG_TempNoAccess, edbrowseTempFile);
1040
1022
            goto abort;
1041
1023
        }
1042
1024
        if(read(fh, serverData, n) < n) {
1043
 
            setError("cannot read the uncompressed web page in %s",
1044
 
               edbrowseTempFile);
 
1025
            setError(MSG_TempNoRead, edbrowseTempFile);
1045
1026
            close(fh);
1046
1027
            goto abort;
1047
1028
        }
1058
1039
    return true;
1059
1040
 
1060
1041
  nohead:
1061
 
    debugPrint(0, "warning: page does not have a recognizable http header");
 
1042
    i_puts(MSG_HTTPHeader);
1062
1043
    return true;
1063
1044
 
1064
1045
  abort:
1146
1127
    int c;
1147
1128
    static const char npf[] = "not a plain file.";
1148
1129
    const int npfsize = strlen(npf);
1149
 
    static const char *const failmessages[] = { 0,
1150
 
        "could not connect to remote host",
1151
 
        "could not connect to remote host (timed out)",
1152
 
        "transfer failed",
1153
 
        "transfer failed (timed out)",
1154
 
        "no such directory",
1155
 
        "could not change directory (timed out)",
1156
 
        "malformed url",
1157
 
        "usage error",
1158
 
        "error in login config file",
1159
 
        "library initialization failed",
1160
 
        "session initialization failed",
1161
 
    };
1162
1130
 
1163
1131
    serverData = 0;
1164
1132
    serverDataLen = 0;
1165
1133
    fileSize = -1;
1166
 
    debugPrint(1, "ftp download");
 
1134
    if(debugLevel >= 1)
 
1135
        i_puts(MSG_FTPDownload);
1167
1136
    dirmode = false;
1168
1137
 
1169
 
  top:
1170
 
    cmd = initString(&cmd_l);
 
1138
  top:cmd = initString(&cmd_l);
1171
1139
    if(dirmode) {
1172
1140
        stringAndString(&cmd, &cmd_l, "ncftpls -l ");
1173
1141
    } else {
1191
1159
 
1192
1160
    f = popen(cmd, "r");
1193
1161
    if(!f) {
1194
 
        setError("could not spawn subcommand %s, errno %d", cmd, errno);
 
1162
        setError(MSG_TempNoSystem, cmd, errno);
1195
1163
        nzFree(cmd);
1196
1164
        return false;
1197
1165
    }
1239
1207
        if(!(rc & 0xff))
1240
1208
            rc >>= 8;
1241
1209
        if(rc > 0 && rc <= 11)
1242
 
            setError(failmessages[rc]);
 
1210
            setError(MSG_FTPConnect - 1 + rc);
1243
1211
        else
1244
 
            setError("unexpected ftp error %d", rc);
 
1212
            setError(MSG_FTPUnexpected, rc);
1245
1213
        return false;
1246
1214
    }
1247
 
    puts(dirmode ? "directory" : "success");
 
1215
    i_puts(dirmode + MSG_Success);
1248
1216
    if(dirmode) {               /* need a final slash */
1249
1217
        int l = strlen(url);
1250
1218
        changeFileName = allocMem(l + 2);
1316
1284
                            }
1317
1285
                        }
1318
1286
                    }           /* valid ip */
1319
 
                }               /* different domain */
 
1287
                }
 
1288
                /* different domain */
1320
1289
            }                   /* valid domain */
1321
1290
            nzFree(href);
1322
1291
            if(iptotal == 5)