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

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

Webmaster