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

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

Webmaster