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

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

Webmaster