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

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

Webmaster