Annotation of libwww/Library/src/HTDialog.c, revision 2.3

2.1       frystyk     1: /*                                                                  HTDialog.c
                      2: **     MESSAGES AND DIALOGS
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: **     This module provides a default implementation of the application part
                      8: **     of the Message and Dialog part or the Library. You do not have to do
                      9: **     it like this - you can use exactly the model for mapping codes into
                     10: **     messages as you like
                     11: **
                     12: ** Authors
                     13: **     HFN     Henrik Frystyk
                     14: */
                     15: 
                     16: /* Library include files */
                     17: #include "WWWLib.h"
                     18: #include "WWWApp.h"
                     19: #include "HTReqMan.h"
                     20: #include "HTDialog.h"                                   /* Implemented here */
                     21: 
                     22: /*
                     23: ** Dialog Messages
                     24: */
                     25: PRIVATE char *HTDialogs[HT_MSG_ELEMENTS] = {
                     26:     "Please enter username:",
                     27:     "Password:",
                     28:     "Please give name of file to save in:",
                     29:     "Plase enter account:",
                     30:     "It might not be allowed to use this method, continue?",
                     31:     "Location has moved, continue?",
                     32:     "A new set of rules is to be added to your setup - continue?"
                     33: };
                     34: 
                     35: /*
                     36: ** All errors that are not strictly HTTP errors but originates from, e.g., 
                     37: ** the FTP protocol all have element numbers > HTERR_HTTP_CODES_END, i.e.,
                     38: ** they should be placed after the blank line
                     39: */
                     40: typedef struct _HTErrorMessage {
                     41:     int        code;                   /* Error number */
                     42:     char *     msg;                    /* Short explanation */
                     43:     char *     url;                    /* Explaning URL */
                     44: } HTErrorMessage;
                     45: 
                     46: PRIVATE HTErrorMessage HTErrors[HTERR_ELEMENTS] = {
                     47: 
                     48: /*    CODE  ERROR MESSAGE                              ERROR URL */
                     49: 
                     50:     { 200, "OK",                                       "ok" },
                     51:     { 201, "Created",                                  "created" },
                     52:     { 202, "Accepted",                                         "accepted" },
                     53:     { 203, "Provisional Information",                  "partial" },
                     54:     { 204, "No Content",                               "no_response" },
                     55:     { 301, "Moved Permanently",                                "moved" },
                     56:     { 302, "Moved Temporarily",                        "moved" },
                     57:     { 303, "Method",                                   "method" },
                     58:     { 304, "Not Modified",                             "not_modified" },
                     59:     { 400, "Bad Request",                              "bad_request" },
                     60:     { 401, "Unauthorized",                             "unauthorized" },
                     61:     { 402, "Payment Required",                                 "payment" },
                     62:     { 403, "Forbidden",                                "forbidden" },
                     63:     { 404, "Not Found",                                        "not_found" },
                     64:     { 405, "Method Not Allowed",                       "method" },
                     65:     { 406, "None Acceptable",                          "none" },
                     66:     { 407, "Proxy Authentication Required",            "proxy" },
                     67:     { 408, "Request Timeout",                          "timeout" },
                     68:     { 500, "Internal Server Error",                    "internal" },
                     69:     { 501, "Not Implemented",                          "not_made" },
                     70:     { 502, "Bad Gateway",                              "bad_gate" },
                     71:     { 503, "Service Unavailable",                      "unavailable" },
                     72:     { 504, "Gateway Timeout",                          "timeout" },
                     73:     { 0,   "-------------------------", "----------------------------------" },
                     74:     { 0,   "Can't locate remote host",                         "host" },
                     75:     { 0,   "No host name found",                       "host" },
                     76:     { 0,   "No file name found or file not accessible", "file" },
                     77:     { 0,   "FTP-server replies",                       "ftp" },
                     78:     { 0,   "FTP-server doesn't reply",                         "no_server" },
                     79:     { 0,   "Server timed out",                                 "time_out" },
                     80:     { 0,   "Gopher-server replies",                    "gopher" },
                     81:     { 0,   "Data transfer interrupted",                "interrupt" },
                     82:     { 0,   "Connection establishment interrupted",     "interrupt" },
                     83:     { 0,   "CSO-server replies",                       "cso" },
                     84:     { 0,   "This is probably a HTTP server 0.9 or less","http_version" },
                     85:     { 0,   "Bad, Incomplete, or Unknown Response",     "bad_reply" },
                     86:     { 0,   "Unknown access authentication scheme",     "control" },
                     87:     { 0,   "News-server replies",                      "news" },
                     88:     { 0,   "Trying `ftp://' instead of `file://'",     "ftpfile" },
                     89:     { 0,   "Too many redirections",                    "redirections" },
                     90:     { 0,   "Premature End Of File",                    "EOF" },
                     91:     { 0,   "Response from WAIS Server too Large - Extra lines ignored",
                     92:                                                        "wais" },
                     93:     { 0,   "WAIS-server doesn't return any data",      "wais" },
                     94:     { 0,   "Can't connect to WAIS-server",             "wais" },
                     95:     { 0,   "System replies",                           "system" },
                     96:     { 0,   "Wrong or unknown access scheme",           "class" },
                     97:     { 0,   "Access scheme not allowed in this context",        "class" },
                     98:     { 0,   "When you are connected, you can log in",   "telnet" }
                     99: };
                    100: 
                    101: /* ------------------------------------------------------------------------- */
                    102: 
                    103: PUBLIC BOOL HTProgress (HTRequest * request, HTAlertOpcode op,
                    104:                        int msgnum, CONST char * dfault, void * input,
                    105:                        HTAlertPar * reply)
                    106: {
                    107:     if (!request) {
                    108:        if (WWWTRACE) TTYPrint(TDEST, "HTProgress.. Bad argument\n");
                    109:        return NO;
                    110:     }
                    111:     switch (op) {
                    112:       case HT_PROG_DNS:
                    113:        TTYPrint(TDEST, "Looking up %s\n", (char *) input);
                    114:        break;
                    115: 
                    116:       case HT_PROG_CONNECT:
                    117:        TTYPrint(TDEST, "Contacting host...\n");
                    118:        break;
                    119: 
                    120:       case HT_PROG_ACCEPT:
                    121:        TTYPrint(TDEST, "Waiting for connection...\n");
                    122:        break;
                    123: 
                    124:       case HT_PROG_READ:
                    125:        {
                    126:            long cl = HTAnchor_length(HTRequest_anchor(request));
                    127:            if (cl > 0) {
                    128:                long b_read = HTRequest_bytesRead(request);
                    129:                double pro = (double) b_read/cl*100;
                    130:                char buf[10];
                    131:                HTNumToStr((unsigned long) cl, buf, 10);
                    132:                TTYPrint(TDEST, "Read (%d%% of %s)\n", (int) pro, buf);
                    133:            } else
                    134:                TTYPrint(TDEST, "Reading...\n");
                    135:        }
                    136:        break;
                    137: 
                    138:       case HT_PROG_WRITE:
                    139:        TTYPrint(TDEST, "Writing...\n");
                    140:        break;
                    141: 
                    142:       case HT_PROG_DONE:
                    143:        TTYPrint(TDEST, "Finished\n");
                    144:        break;
                    145: 
                    146:       case HT_PROG_WAIT:
                    147:        TTYPrint(TDEST, "Waiting for free socket...\n");
                    148:        break;
                    149: 
                    150:       default:
                    151:        TTYPrint(TDEST, "UNKNOWN PROGRESS STATE\n");
                    152:        break;
                    153:     }
                    154:     return YES;
                    155: }
                    156: 
                    157: PUBLIC BOOL HTConfirm (HTRequest * request, HTAlertOpcode op,
                    158:                       int msgnum, CONST char * dfault, void * input,
                    159:                       HTAlertPar * reply)
                    160: {
                    161:     char response[4];  /* One more for terminating NULL -- AL */
                    162:     TTYPrint(TDEST, "%s", HTDialogs[msgnum]);
                    163:     if (input) TTYPrint(TDEST, " (%s)", (char *) input);
                    164:     TTYPrint(TDEST, " (y/n) ");
                    165: #ifndef NO_STDIO
2.2       frystyk   166:     if (fgets(response, 4, stdin))                /* get reply, max 3 chars */
2.1       frystyk   167: #endif
2.2       frystyk   168:     {
2.1       frystyk   169:        char *ptr = response;
                    170:        while (*ptr) {
                    171:            if (*ptr == '\n') {
                    172:                *ptr = '\0';
                    173:                break;
                    174:            }
                    175:            *ptr = TOUPPER(*ptr);
                    176:            ptr++;
                    177:        }
                    178:        return (!strcmp(response, "YES") || !strcmp(response, "Y")) ? YES : NO;
                    179:     }
                    180:     return NO;
                    181: }
                    182: 
                    183: /*     Prompt for answer and get text back. Reply text is either NULL on
                    184: **     error or a dynamic string which the caller must free.
                    185: */
                    186: PUBLIC BOOL HTPrompt (HTRequest * request, HTAlertOpcode op,
                    187:                      int msgnum, CONST char * dfault, void * input,
                    188:                      HTAlertPar * reply)
                    189: {
                    190:     char buffer[200];
                    191:     TTYPrint(TDEST, "%s ", HTDialogs[msgnum]);
                    192:     if (input) TTYPrint(TDEST, " (%s) ", (char *) input);
                    193:     if (dfault) TTYPrint(TDEST, "(RETURN for [%s]) ", (char *) dfault);
                    194:     if (reply && msgnum>=0) {
                    195: #ifndef NO_STDIO
                    196:        if (!fgets(buffer, 200, stdin)) return NO;
                    197:        buffer[strlen(buffer)-1] = '\0';                /* Overwrite newline */
                    198:        if (*buffer)
                    199:            HTAlert_setReplyMessage(reply, buffer);
                    200:        else if (dfault)
                    201:            HTAlert_setReplyMessage(reply, (char *) dfault);
                    202:        else
                    203:            return NO;
                    204:        return YES;
                    205: #endif
                    206:     }
                    207:     return NO;
                    208: }
                    209: 
                    210: /*     Prompt for password without echoing the reply. Reply text is
                    211: **     either NULL on error or a dynamic string which the caller must free.
                    212: */
                    213: PUBLIC BOOL HTPromptPassword (HTRequest * request, HTAlertOpcode op,
                    214:                              int msgnum, CONST char * dfault, void * input,
                    215:                              HTAlertPar * reply)
                    216: {
                    217:     if (reply && msgnum>=0) {
                    218: #ifndef NO_PASSWD
                    219:        char * pw = (char *) getpass(HTDialogs[msgnum]);
2.2       frystyk   220:        if (pw) HTAlert_setReplySecret(reply, pw);
2.1       frystyk   221:        return YES;
                    222: #endif
                    223:     }
                    224:     return NO;
                    225: }
                    226: 
                    227: /*     Username and password
                    228: **     ---------------------
                    229: **     Prompt Username and password as an atomic operation
                    230: */
                    231: PUBLIC BOOL HTPromptUsernameAndPassword (HTRequest * request, HTAlertOpcode op,
                    232:                                         int msgnum, CONST char * dfault,
                    233:                                         void * input, HTAlertPar * reply)
                    234: {
                    235:     BOOL status = HTPrompt(request, op, HT_MSG_UID, dfault, input, reply);
                    236:     return status ?
                    237:        HTPromptPassword(request, op, HT_MSG_PW, dfault, input, reply) : NO;
                    238: }
                    239: 
                    240: /*     HTError_print
                    241: **     -------------
                    242: **     Default function that creates an error message using HTAlert() to
                    243: **     put out the contents of the error_stack messages. Furthermore, the
                    244: **     error_info structure contains a name of a help file that might be put
                    245: **     up as a link. This file can then be multi-linguistic.
                    246: */
                    247: PUBLIC BOOL HTError_print (HTRequest * request, HTAlertOpcode op,
                    248:                           int msgnum, CONST char * dfault, void * input,
                    249:                           HTAlertPar * reply)
                    250: {
                    251:     HTList *cur = (HTList *) input;
                    252:     HTError *pres;
                    253:     HTErrorShow showmask = HTError_show();
                    254:     HTChunk *msg = NULL;
                    255:     int code;
                    256:     if (WWWTRACE) TTYPrint(TDEST, "HTError..... Generating message\n");
                    257:     if (!request || !cur) return NO;
                    258:     while ((pres = (HTError *) HTList_nextObject(cur))) {
                    259:        int index = HTError_index(pres);
                    260:        if (HTError_doShow(pres)) {
                    261:            if (!msg) {
                    262:                HTSeverity severity = HTError_severity(pres);
2.3     ! frystyk   263:                msg = HTChunk_new(128);
2.1       frystyk   264:                if (severity == ERR_WARN)
2.3     ! frystyk   265:                    HTChunk_puts(msg, "Warning: ");
2.1       frystyk   266:                else if (severity == ERR_NON_FATAL)
2.3     ! frystyk   267:                    HTChunk_puts(msg, "Non Fatal Error: ");
2.1       frystyk   268:                else if (severity == ERR_FATAL)
2.3     ! frystyk   269:                    HTChunk_puts(msg, "Fatal Error: ");
2.1       frystyk   270:                else if (severity == ERR_INFO)
2.3     ! frystyk   271:                    HTChunk_puts(msg, "Information: ");
2.1       frystyk   272:                else {
                    273:                    if (WWWTRACE)
                    274:                        TTYPrint(TDEST, "HTError..... Unknown Classification of Error (%d)...\n", severity);
2.3     ! frystyk   275:                    HTChunk_delete(msg);
2.1       frystyk   276:                    return NO;
                    277:                }
                    278: 
                    279:                /* Error number */
                    280:                if ((code = HTErrors[index].code) > 0) {
                    281:                    char buf[10];
                    282:                    sprintf(buf, "%d ", code);
2.3     ! frystyk   283:                    HTChunk_puts(msg, buf);
2.1       frystyk   284:                }
                    285:            } else
2.3     ! frystyk   286:                HTChunk_puts(msg, "\nReason: ");
        !           287:            HTChunk_puts(msg, HTErrors[index].msg);         /* Error message */
2.1       frystyk   288: 
                    289:            if (showmask & HT_ERR_SHOW_PARS) {           /* Error parameters */
                    290:                int length;
                    291:                int cnt;                
                    292:                char *pars = (char *) HTError_parameter(pres, &length);
                    293:                if (length && pars) {
2.3     ! frystyk   294:                    HTChunk_puts(msg, " (");
2.1       frystyk   295:                    for (cnt=0; cnt<length; cnt++) {
                    296:                        char ch = *(pars+cnt);
                    297:                        if (ch < 0x20 || ch >= 0x7F)
2.3     ! frystyk   298:                            HTChunk_putc(msg, '#');
2.1       frystyk   299:                        else
2.3     ! frystyk   300:                            HTChunk_putc(msg, ch);
2.1       frystyk   301:                    }
2.3     ! frystyk   302:                    HTChunk_puts(msg, ") ");
2.1       frystyk   303:                }
                    304:            }
                    305: 
                    306:            if (showmask & HT_ERR_SHOW_LOCATION) {         /* Error Location */
2.3     ! frystyk   307:                HTChunk_puts(msg, "This occured in ");
        !           308:                HTChunk_puts(msg, HTError_location(pres));
        !           309:                HTChunk_putc(msg, '\n');
2.1       frystyk   310:            }
                    311: 
                    312:            /*
                    313:            ** Make sure that we don't get this error more than once even
                    314:            ** if we are keeping the error stack from one request to another
                    315:            */
                    316:            HTError_setIgnore(pres);
                    317:            
                    318:            /* If we only are show the most recent entry then break here */
                    319:            if (showmask & HT_ERR_SHOW_FIRST)
                    320:                break;
                    321:        }
                    322:     }
                    323:     if (msg) {
2.3     ! frystyk   324:        HTChunk_putc(msg, '\n');
        !           325:        TTYPrint(TDEST, "WARNING: %s\n", HTChunk_data(msg));
        !           326:        HTChunk_delete(msg);
2.1       frystyk   327:     }
                    328:     return YES;
                    329: }
                    330: 

Webmaster