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

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

Webmaster