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

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.
2.20    ! frystyk     6: **     @(#) $Id: HTDialog.c,v 2.19 1996/08/05 17:22:14 frystyk Exp $
2.1       frystyk     7: **
                      8: **     This module provides a default implementation of the application part
                      9: **     of the Message and Dialog part or the Library. You do not have to do
                     10: **     it like this - you can use exactly the model for mapping codes into
                     11: **     messages as you like
                     12: **
                     13: ** Authors
                     14: **     HFN     Henrik Frystyk
                     15: */
                     16: 
                     17: /* Library include files */
                     18: #include "WWWLib.h"
                     19: #include "WWWApp.h"
2.6       frystyk    20: #include "WWWHTTP.h"
2.1       frystyk    21: #include "HTDialog.h"                                   /* Implemented here */
                     22: 
                     23: /*
                     24: ** Dialog Messages
                     25: */
2.18      frystyk    26: PRIVATE const char * HTDialogs[HT_MSG_ELEMENTS] = {
2.1       frystyk    27:     "Please enter username:",
2.20    ! frystyk    28:     "Please enter username for proxy authentication:",
2.1       frystyk    29:     "Password:",
                     30:     "Please give name of file to save in:",
                     31:     "Plase enter account:",
2.19      frystyk    32:     "You might not be allowed to use this method here, continue?",
2.1       frystyk    33:     "Location has moved, continue?",
2.17      frystyk    34:     "A new set of rules is to be added to your setup - continue?",
                     35:     "This file already exists - replace existing file?",
2.18      frystyk    36:     "Authentication failed - retry?",
2.19      frystyk    37:     "Proxy authentication failed - retry?",
2.18      frystyk    38:     "This method has already been performed - repeat operation?",
                     39:     "This document is very big - continue operation?",
                     40:     "The source document for this operation has moved - continue operation with new location?",
                     41:     "The destination document for this operation has moved - continue operation with new location?",
2.19      frystyk    42:     "A redirection may change the behavior of this method - proceed anyway?"
2.1       frystyk    43: };
                     44: 
                     45: /*
                     46: ** All errors that are not strictly HTTP errors but originates from, e.g., 
                     47: ** the FTP protocol all have element numbers > HTERR_HTTP_CODES_END, i.e.,
                     48: ** they should be placed after the blank line
                     49: */
                     50: typedef struct _HTErrorMessage {
                     51:     int        code;                   /* Error number */
                     52:     char *     msg;                    /* Short explanation */
                     53:     char *     url;                    /* Explaning URL */
                     54: } HTErrorMessage;
                     55: 
                     56: PRIVATE HTErrorMessage HTErrors[HTERR_ELEMENTS] = {
                     57: 
                     58: /*    CODE  ERROR MESSAGE                              ERROR URL */
                     59: 
2.15      frystyk    60:     { 100, "Continue",                                         "information" },
                     61:     { 101, "Switching Protocols",                      "information" },
                     62:     { 200, "OK",                                       "success" },
                     63:     { 201, "Created",                                  "success" },
                     64:     { 202, "Accepted",                                         "success" },
                     65:     { 203, "Non-authoritative Information",            "success" },
                     66:     { 204, "No Content",                               "success" },
                     67:     { 205, "Reset Content",                            "success" },
                     68:     { 206, "Partial Content",                          "success" },
                     69:     { 300, "Multiple Choices",                         "redirection" },
                     70:     { 301, "Moved Permanently",                                "redirection" },
                     71:     { 302, "Moved Temporarily",                        "redirection" },
                     72:     { 303, "See Other",                                        "redirection" },
                     73:     { 304, "Not Modified",                             "redirection" },
                     74:     { 305, "Use Proxy",                                        "redirection" },
                     75:     { 400, "Bad Request",                              "client_error" },
                     76:     { 401, "Unauthorized",                             "client_error" },
                     77:     { 402, "Payment Required",                                 "client_error" },
                     78:     { 403, "Forbidden",                                "client_error" },
                     79:     { 404, "Not Found",                                        "client_error" },
                     80:     { 405, "Method Not Allowed",                       "client_error" },
                     81:     { 406, "Not Acceptable",                           "client_error" },
                     82:     { 407, "Proxy Authentication Required",            "client_error" },
                     83:     { 408, "Request Timeout",                          "client_error" },
                     84:     { 409, "Conflict",                                 "client_error" },
                     85:     { 410, "Gone",                                     "client_error" },
                     86:     { 411, "Length Required",                          "client_error" },
                     87:     { 412, "Precondition Failed",                      "client_error" },
                     88:     { 413, "Request Entity Too Large",                 "client_error" },
                     89:     { 414, "Request-URI Too Large",                    "client_error" },
                     90:     { 415, "Unsupported Media Type",                   "client_error" },
                     91:     { 500, "Internal Server Error",                    "server_error" },
                     92:     { 501, "Not Implemented",                          "server_error" },
                     93:     { 502, "Bad Gateway",                              "server_error" },
                     94:     { 503, "Service Unavailable",                      "server_error" },
                     95:     { 504, "Gateway Timeout",                          "server_error" },
                     96:     { 505, "HTTP Version not supported",               "server_error" },
                     97: 
                     98:     /* Cache Warnings */
                     99:     { 10,  "Response is Stale",                                "cache" },
                    100:     { 11,  "Revalidation Failed",                      "cache" },
                    101:     { 12,  "Disconnected Opeartion",                   "cache" },
                    102:     { 13,  "Heuristic Expiration",                     "cache" },
                    103:     { 14,  "Transformation Applied",                   "cache" },
                    104:     { 99,  "Cache warning",                            "cache" },
                    105: 
                    106:     /* Non-HTTP Error codes and warnings */
                    107:     { 0,   "Can't locate remote host",                         "internal" },
                    108:     { 0,   "No host name found",                       "internal" },
                    109:     { 0,   "No file name found or file not accessible", "internal" },
                    110:     { 0,   "FTP-server replies",                       "internal" },
                    111:     { 0,   "FTP-server doesn't reply",                         "internal" },
                    112:     { 0,   "Server timed out",                                 "internal" },
                    113:     { 0,   "Gopher-server replies",                    "internal" },
                    114:     { 0,   "Data transfer interrupted",                "internal" },
                    115:     { 0,   "Connection establishment interrupted",     "internal" },
                    116:     { 0,   "CSO-server replies",                       "internal" },
                    117:     { 0,   "This is probably a HTTP server 0.9 or less","internal" },
                    118:     { 0,   "Bad, Incomplete, or Unknown Response",     "internal" },
                    119:     { 0,   "Unknown access authentication scheme",     "internal" },
                    120:     { 0,   "News-server replies",                      "internal" },
                    121:     { 0,   "Trying `ftp://' instead of `file://'",     "internal" },
                    122:     { 0,   "Too many redirections",                    "internal" },
2.16      frystyk   123:     { 0,   "Method not suited for automatic redirection","internal" },
2.15      frystyk   124:     { 0,   "Premature End Of File",                    "internal" },
                    125:     { 0,   "Response from WAIS Server too Large - Extra lines ignored","internal"},
                    126:     { 0,   "WAIS-server doesn't return any data",      "internal" },
                    127:     { 0,   "Can't connect to WAIS-server",             "internal" },
                    128:     { 0,   "System replies",                           "internal" },
                    129:     { 0,   "Wrong or unknown access scheme",           "internal" },
                    130:     { 0,   "Access scheme not allowed in this context",        "internal" },
                    131:     { 0,   "When you are connected, you can log in",   "internal" }
2.1       frystyk   132: };
                    133: 
                    134: /* ------------------------------------------------------------------------- */
                    135: 
                    136: PUBLIC BOOL HTProgress (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   137:                        int msgnum, const char * dfault, void * input,
2.1       frystyk   138:                        HTAlertPar * reply)
                    139: {
2.19      frystyk   140:     if (!request || HTRequest_internal(request)) return NO;
2.1       frystyk   141:     switch (op) {
                    142:       case HT_PROG_DNS:
2.9       frystyk   143:        HTTrace("Looking up %s\n", input ? (char *) input : "");
2.1       frystyk   144:        break;
                    145: 
                    146:       case HT_PROG_CONNECT:
2.9       frystyk   147:        HTTrace("Contacting %s\n", input ? (char *) input : "");
2.1       frystyk   148:        break;
                    149: 
                    150:       case HT_PROG_ACCEPT:
2.8       eric      151:        HTTrace("Waiting for connection...\n");
2.1       frystyk   152:        break;
                    153: 
                    154:       case HT_PROG_READ:
                    155:        {
                    156:            long cl = HTAnchor_length(HTRequest_anchor(request));
                    157:            if (cl > 0) {
                    158:                long b_read = HTRequest_bytesRead(request);
                    159:                double pro = (double) b_read/cl*100;
                    160:                char buf[10];
                    161:                HTNumToStr((unsigned long) cl, buf, 10);
2.8       eric      162:                HTTrace("Read (%d%% of %s)\n", (int) pro, buf);
2.1       frystyk   163:            } else
2.8       eric      164:                HTTrace("Reading...\n");
2.1       frystyk   165:        }
                    166:        break;
                    167: 
                    168:       case HT_PROG_WRITE:
2.4       frystyk   169:        if (HTRequest_isPostWeb(request)) {
                    170:            HTParentAnchor *anchor=HTRequest_anchor(HTRequest_source(request));
                    171:            long cl = HTAnchor_length(anchor);
                    172:            if (cl > 0) {
                    173:                long b_write = HTRequest_bytesWritten(request);
                    174:                double pro = (double) b_write/cl*100;
                    175:                char buf[10];
                    176:                HTNumToStr((unsigned long) cl, buf, 10);
2.8       eric      177:                HTTrace("Written (%d%% of %s)\n", (int) pro, buf);
2.4       frystyk   178:            } else
2.8       eric      179:                HTTrace("Writing...\n");
2.4       frystyk   180:        }
2.1       frystyk   181:        break;
                    182: 
                    183:       case HT_PROG_DONE:
2.8       eric      184:        HTTrace("Finished\n");
2.1       frystyk   185:        break;
                    186: 
                    187:       case HT_PROG_WAIT:
2.12      frystyk   188:        HTTrace("Waiting for free socket...\n");
2.1       frystyk   189:        break;
                    190: 
                    191:       default:
2.8       eric      192:        HTTrace("UNKNOWN PROGRESS STATE\n");
2.1       frystyk   193:        break;
                    194:     }
                    195:     return YES;
                    196: }
                    197: 
                    198: PUBLIC BOOL HTConfirm (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   199:                       int msgnum, const char * dfault, void * input,
2.1       frystyk   200:                       HTAlertPar * reply)
                    201: {
                    202:     char response[4];  /* One more for terminating NULL -- AL */
2.8       eric      203:     HTTrace("%s", HTDialogs[msgnum]);
                    204:     if (input) HTTrace(" (%s)", (char *) input);
                    205:     HTTrace(" (y/n) ");
2.1       frystyk   206: #ifndef NO_STDIO
2.2       frystyk   207:     if (fgets(response, 4, stdin))                /* get reply, max 3 chars */
2.1       frystyk   208: #endif
2.2       frystyk   209:     {
2.1       frystyk   210:        char *ptr = response;
                    211:        while (*ptr) {
                    212:            if (*ptr == '\n') {
                    213:                *ptr = '\0';
                    214:                break;
                    215:            }
                    216:            *ptr = TOUPPER(*ptr);
                    217:            ptr++;
                    218:        }
                    219:        return (!strcmp(response, "YES") || !strcmp(response, "Y")) ? YES : NO;
                    220:     }
                    221:     return NO;
                    222: }
                    223: 
                    224: /*     Prompt for answer and get text back. Reply text is either NULL on
2.10      frystyk   225: **     error or a dynamic string which the caller must HT_FREE.
2.1       frystyk   226: */
                    227: PUBLIC BOOL HTPrompt (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   228:                      int msgnum, const char * dfault, void * input,
2.1       frystyk   229:                      HTAlertPar * reply)
                    230: {
2.8       eric      231:     HTTrace("%s ", HTDialogs[msgnum]);
                    232:     if (input) HTTrace(" (%s) ", (char *) input);
                    233:     if (dfault) HTTrace("(RETURN for [%s]) ", (char *) dfault);
2.1       frystyk   234:     if (reply && msgnum>=0) {
                    235: #ifndef NO_STDIO
2.5       frystyk   236:         char buffer[200];
2.1       frystyk   237:        if (!fgets(buffer, 200, stdin)) return NO;
                    238:        buffer[strlen(buffer)-1] = '\0';                /* Overwrite newline */
                    239:        if (*buffer)
                    240:            HTAlert_setReplyMessage(reply, buffer);
                    241:        else if (dfault)
                    242:            HTAlert_setReplyMessage(reply, (char *) dfault);
                    243:        else
                    244:            return NO;
                    245:        return YES;
                    246: #endif
                    247:     }
                    248:     return NO;
                    249: }
                    250: 
                    251: /*     Prompt for password without echoing the reply. Reply text is
2.10      frystyk   252: **     either NULL on error or a dynamic string which the caller must HT_FREE.
2.1       frystyk   253: */
                    254: PUBLIC BOOL HTPromptPassword (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   255:                              int msgnum, const char * dfault, void * input,
2.1       frystyk   256:                              HTAlertPar * reply)
                    257: {
                    258:     if (reply && msgnum>=0) {
2.10      frystyk   259: #ifdef HAVE_GETPASS
2.1       frystyk   260:        char * pw = (char *) getpass(HTDialogs[msgnum]);
2.2       frystyk   261:        if (pw) HTAlert_setReplySecret(reply, pw);
2.1       frystyk   262:        return YES;
2.10      frystyk   263: #else
2.11      eric      264:        return NO;      /* needed for WWW_MSWINDOWS */
2.10      frystyk   265: #endif /* HAVE_GETPASS */
2.1       frystyk   266:     }
                    267:     return NO;
                    268: }
                    269: 
                    270: /*     Username and password
                    271: **     ---------------------
                    272: **     Prompt Username and password as an atomic operation
                    273: */
                    274: PUBLIC BOOL HTPromptUsernameAndPassword (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   275:                                         int msgnum, const char * dfault,
2.1       frystyk   276:                                         void * input, HTAlertPar * reply)
                    277: {
                    278:     BOOL status = HTPrompt(request, op, HT_MSG_UID, dfault, input, reply);
                    279:     return status ?
                    280:        HTPromptPassword(request, op, HT_MSG_PW, dfault, input, reply) : NO;
                    281: }
                    282: 
                    283: /*     HTError_print
                    284: **     -------------
                    285: **     Default function that creates an error message using HTAlert() to
                    286: **     put out the contents of the error_stack messages. Furthermore, the
                    287: **     error_info structure contains a name of a help file that might be put
                    288: **     up as a link. This file can then be multi-linguistic.
                    289: */
                    290: PUBLIC BOOL HTError_print (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   291:                           int msgnum, const char * dfault, void * input,
2.1       frystyk   292:                           HTAlertPar * reply)
                    293: {
                    294:     HTList *cur = (HTList *) input;
                    295:     HTError *pres;
                    296:     HTErrorShow showmask = HTError_show();
                    297:     HTChunk *msg = NULL;
                    298:     int code;
2.8       eric      299:     if (WWWTRACE) HTTrace("HTError..... Generating message\n");
2.1       frystyk   300:     if (!request || !cur) return NO;
                    301:     while ((pres = (HTError *) HTList_nextObject(cur))) {
                    302:        int index = HTError_index(pres);
                    303:        if (HTError_doShow(pres)) {
                    304:            if (!msg) {
                    305:                HTSeverity severity = HTError_severity(pres);
2.3       frystyk   306:                msg = HTChunk_new(128);
2.1       frystyk   307:                if (severity == ERR_WARN)
2.3       frystyk   308:                    HTChunk_puts(msg, "Warning: ");
2.1       frystyk   309:                else if (severity == ERR_NON_FATAL)
2.3       frystyk   310:                    HTChunk_puts(msg, "Non Fatal Error: ");
2.1       frystyk   311:                else if (severity == ERR_FATAL)
2.3       frystyk   312:                    HTChunk_puts(msg, "Fatal Error: ");
2.1       frystyk   313:                else if (severity == ERR_INFO)
2.3       frystyk   314:                    HTChunk_puts(msg, "Information: ");
2.1       frystyk   315:                else {
                    316:                    if (WWWTRACE)
2.8       eric      317:                        HTTrace("HTError..... Unknown Classification of Error (%d)...\n", severity);
2.3       frystyk   318:                    HTChunk_delete(msg);
2.1       frystyk   319:                    return NO;
                    320:                }
                    321: 
                    322:                /* Error number */
                    323:                if ((code = HTErrors[index].code) > 0) {
                    324:                    char buf[10];
                    325:                    sprintf(buf, "%d ", code);
2.3       frystyk   326:                    HTChunk_puts(msg, buf);
2.1       frystyk   327:                }
                    328:            } else
2.3       frystyk   329:                HTChunk_puts(msg, "\nReason: ");
                    330:            HTChunk_puts(msg, HTErrors[index].msg);         /* Error message */
2.1       frystyk   331: 
                    332:            if (showmask & HT_ERR_SHOW_PARS) {           /* Error parameters */
                    333:                int length;
                    334:                int cnt;                
                    335:                char *pars = (char *) HTError_parameter(pres, &length);
                    336:                if (length && pars) {
2.3       frystyk   337:                    HTChunk_puts(msg, " (");
2.1       frystyk   338:                    for (cnt=0; cnt<length; cnt++) {
                    339:                        char ch = *(pars+cnt);
                    340:                        if (ch < 0x20 || ch >= 0x7F)
2.3       frystyk   341:                            HTChunk_putc(msg, '#');
2.1       frystyk   342:                        else
2.3       frystyk   343:                            HTChunk_putc(msg, ch);
2.1       frystyk   344:                    }
2.3       frystyk   345:                    HTChunk_puts(msg, ") ");
2.1       frystyk   346:                }
                    347:            }
                    348: 
                    349:            if (showmask & HT_ERR_SHOW_LOCATION) {         /* Error Location */
2.3       frystyk   350:                HTChunk_puts(msg, "This occured in ");
                    351:                HTChunk_puts(msg, HTError_location(pres));
                    352:                HTChunk_putc(msg, '\n');
2.1       frystyk   353:            }
                    354: 
                    355:            /*
                    356:            ** Make sure that we don't get this error more than once even
                    357:            ** if we are keeping the error stack from one request to another
                    358:            */
                    359:            HTError_setIgnore(pres);
                    360:            
                    361:            /* If we only are show the most recent entry then break here */
                    362:            if (showmask & HT_ERR_SHOW_FIRST)
                    363:                break;
                    364:        }
                    365:     }
                    366:     if (msg) {
2.3       frystyk   367:        HTChunk_putc(msg, '\n');
2.8       eric      368:        HTTrace("WARNING: %s\n", HTChunk_data(msg));
2.3       frystyk   369:        HTChunk_delete(msg);
2.1       frystyk   370:     }
                    371:     return YES;
                    372: }
                    373: 
2.6       frystyk   374: /*     HTError_response
                    375: **     ----------------
                    376: **     Default function that creates an error message using HTAlert() to
                    377: **     put out the contents of the error_stack messages. Furthermore, the
                    378: **     error_info structure contains a name of a help file that might be put
                    379: **     up as a link. This file can then be multi-linguistic.
                    380: */
                    381: PUBLIC BOOL HTError_response (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   382:                              int msgnum, const char * dfault, void * input,
2.6       frystyk   383:                              HTAlertPar * reply)
                    384: {
                    385:     HTList * cur = (HTList *) input;
                    386:     HTError * pres;
                    387:     HTErrorShow showmask = HTError_show();
                    388:     HTChunk * msg = NULL;
                    389:     int code;
2.8       eric      390:     if (WWWTRACE) HTTrace("HTError..... Generating HTTP response\n");
2.6       frystyk   391:     if (!request || !cur || !reply) return NO;
                    392:     while ((pres = (HTError *) HTList_nextObject(cur))) {
                    393:        int index = HTError_index(pres);
                    394:        if (HTError_doShow(pres)) {
                    395:            if (!msg) {
                    396:                msg = HTChunk_new(128);
                    397:                if ((code = HTErrors[index].code) > 0) {
                    398:                    char * reason = HTErrors[index].msg;
2.7       frystyk   399:                    char * buf;
                    400:                    if ((buf = (char  *) HT_MALLOC(20 + strlen(reason))) == NULL)
                    401:                        HT_OUTOFMEM("HTError_response");
2.6       frystyk   402:                    sprintf(buf,"%s %d %s%c%c",HTTP_VERSION,code,reason,CR,LF);
                    403:                    HTAlert_assignReplyMessage(reply, buf);
                    404:                }
                    405:            } else {
                    406:                HTChunk_puts(msg, "\nReason: ");
                    407:                HTChunk_puts(msg, HTErrors[index].msg);     /* Error message */
                    408:            }
                    409: 
                    410:            if (showmask & HT_ERR_SHOW_PARS) {           /* Error parameters */
                    411:                int length;
                    412:                int cnt;                
                    413:                char *pars = (char *) HTError_parameter(pres, &length);
                    414:                if (length && pars) {
                    415:                    HTChunk_puts(msg, " (");
                    416:                    for (cnt=0; cnt<length; cnt++) {
                    417:                        char ch = *(pars+cnt);
                    418:                        if (ch < 0x20 || ch >= 0x7F)
                    419:                            HTChunk_putc(msg, '#');
                    420:                        else
                    421:                            HTChunk_putc(msg, ch);
                    422:                    }
                    423:                    HTChunk_puts(msg, ") ");
                    424:                }
                    425:            }
                    426: 
                    427:            if (showmask & HT_ERR_SHOW_LOCATION) {         /* Error Location */
                    428:                HTChunk_puts(msg, "This occured in ");
                    429:                HTChunk_puts(msg, HTError_location(pres));
                    430:                HTChunk_putc(msg, '\n');
                    431:            }
                    432: 
                    433:            /*
                    434:            ** Make sure that we don't get this error more than once even
                    435:            ** if we are keeping the error stack from one request to another
                    436:            */
                    437:            HTError_setIgnore(pres);
                    438:            
                    439:            /* If we only are show the most recent entry then break here */
                    440:            if (showmask & HT_ERR_SHOW_FIRST)
                    441:                break;
                    442:        }
                    443:     }
                    444:     if (msg) {
                    445:        HTChunk_putc(msg, '\n');
                    446: #if 0
2.8       eric      447:        HTTrace("WARNING: %s\n", HTChunk_data(msg));
2.6       frystyk   448: #endif
                    449:        HTChunk_delete(msg);
                    450:     }
                    451:     return YES;
                    452: }

Webmaster