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

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

Webmaster