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

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.25    ! eric        6: **     @(#) $Id: HTDialog.c,v 2.24 1996/09/08 22:08:13 frystyk Exp $
2.1       frystyk     7: **
                      8: **     This module provides a default implementation of the application part
                      9: **     of the Message and Dialog part or the Library. You do not have to do
                     10: **     it like this - you can use exactly the model for mapping codes into
                     11: **     messages as you like
                     12: **
                     13: ** Authors
                     14: **     HFN     Henrik Frystyk
                     15: */
                     16: 
                     17: /* Library include files */
                     18: #include "WWWLib.h"
                     19: #include "WWWApp.h"
2.6       frystyk    20: #include "WWWHTTP.h"
2.1       frystyk    21: #include "HTDialog.h"                                   /* Implemented here */
                     22: 
                     23: /*
                     24: ** Dialog Messages
                     25: */
2.25    ! eric       26: PRIVATE const char * HTDialogs[HT_MSG_ELEMENTS] = {HT_MSG_ENGLISH_INITIALIZER};
2.1       frystyk    27: 
                     28: /*
                     29: ** All errors that are not strictly HTTP errors but originates from, e.g., 
                     30: ** the FTP protocol all have element numbers > HTERR_HTTP_CODES_END, i.e.,
                     31: ** they should be placed after the blank line
                     32: */
2.25    ! eric       33: PRIVATE HTErrorMessage HTErrors[HTERR_ELEMENTS] = {HTERR_ENGLISH_INITIALIZER};
2.1       frystyk    34: 
                     35: /* ------------------------------------------------------------------------- */
                     36: 
                     37: PUBLIC BOOL HTProgress (HTRequest * request, HTAlertOpcode op,
2.10      frystyk    38:                        int msgnum, const char * dfault, void * input,
2.1       frystyk    39:                        HTAlertPar * reply)
                     40: {
2.24      frystyk    41:     if (request && HTRequest_internal(request)) return NO;
2.1       frystyk    42:     switch (op) {
                     43:       case HT_PROG_DNS:
2.9       frystyk    44:        HTTrace("Looking up %s\n", input ? (char *) input : "");
2.1       frystyk    45:        break;
                     46: 
                     47:       case HT_PROG_CONNECT:
2.9       frystyk    48:        HTTrace("Contacting %s\n", input ? (char *) input : "");
2.1       frystyk    49:        break;
                     50: 
                     51:       case HT_PROG_ACCEPT:
2.8       eric       52:        HTTrace("Waiting for connection...\n");
2.1       frystyk    53:        break;
                     54: 
                     55:       case HT_PROG_READ:
2.24      frystyk    56:        if (request) {
2.1       frystyk    57:            long cl = HTAnchor_length(HTRequest_anchor(request));
                     58:            if (cl > 0) {
                     59:                long b_read = HTRequest_bytesRead(request);
                     60:                double pro = (double) b_read/cl*100;
                     61:                char buf[10];
                     62:                HTNumToStr((unsigned long) cl, buf, 10);
2.8       eric       63:                HTTrace("Read (%d%% of %s)\n", (int) pro, buf);
2.1       frystyk    64:            } else
2.8       eric       65:                HTTrace("Reading...\n");
2.1       frystyk    66:        }
                     67:        break;
                     68: 
                     69:       case HT_PROG_WRITE:
2.24      frystyk    70:        if (request && HTMethod_hasEntity(HTRequest_method(request))) {
2.4       frystyk    71:            HTParentAnchor *anchor=HTRequest_anchor(HTRequest_source(request));
                     72:            long cl = HTAnchor_length(anchor);
                     73:            if (cl > 0) {
                     74:                long b_write = HTRequest_bytesWritten(request);
                     75:                double pro = (double) b_write/cl*100;
                     76:                char buf[10];
                     77:                HTNumToStr((unsigned long) cl, buf, 10);
2.8       eric       78:                HTTrace("Written (%d%% of %s)\n", (int) pro, buf);
2.4       frystyk    79:            } else
2.8       eric       80:                HTTrace("Writing...\n");
2.4       frystyk    81:        }
2.1       frystyk    82:        break;
                     83: 
                     84:       case HT_PROG_DONE:
2.22      frystyk    85:        HTTrace("Connection closed\n");
2.1       frystyk    86:        break;
                     87: 
                     88:       case HT_PROG_WAIT:
2.12      frystyk    89:        HTTrace("Waiting for free socket...\n");
2.24      frystyk    90:        break;
                     91: 
                     92:       case HT_PROG_GC:
                     93:        HTTrace("Garbage colleting persistent cache - please wait...\n");
2.1       frystyk    94:        break;
                     95: 
                     96:       default:
2.8       eric       97:        HTTrace("UNKNOWN PROGRESS STATE\n");
2.1       frystyk    98:        break;
                     99:     }
                    100:     return YES;
                    101: }
                    102: 
                    103: PUBLIC BOOL HTConfirm (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   104:                       int msgnum, const char * dfault, void * input,
2.1       frystyk   105:                       HTAlertPar * reply)
                    106: {
                    107:     char response[4];  /* One more for terminating NULL -- AL */
2.8       eric      108:     HTTrace("%s", HTDialogs[msgnum]);
                    109:     if (input) HTTrace(" (%s)", (char *) input);
                    110:     HTTrace(" (y/n) ");
2.1       frystyk   111: #ifndef NO_STDIO
2.2       frystyk   112:     if (fgets(response, 4, stdin))                /* get reply, max 3 chars */
2.1       frystyk   113: #endif
2.2       frystyk   114:     {
2.1       frystyk   115:        char *ptr = response;
                    116:        while (*ptr) {
                    117:            if (*ptr == '\n') {
                    118:                *ptr = '\0';
                    119:                break;
                    120:            }
                    121:            *ptr = TOUPPER(*ptr);
                    122:            ptr++;
                    123:        }
                    124:        return (!strcmp(response, "YES") || !strcmp(response, "Y")) ? YES : NO;
                    125:     }
                    126:     return NO;
                    127: }
                    128: 
                    129: /*     Prompt for answer and get text back. Reply text is either NULL on
2.10      frystyk   130: **     error or a dynamic string which the caller must HT_FREE.
2.1       frystyk   131: */
                    132: PUBLIC BOOL HTPrompt (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   133:                      int msgnum, const char * dfault, void * input,
2.1       frystyk   134:                      HTAlertPar * reply)
                    135: {
2.8       eric      136:     HTTrace("%s ", HTDialogs[msgnum]);
                    137:     if (input) HTTrace(" (%s) ", (char *) input);
                    138:     if (dfault) HTTrace("(RETURN for [%s]) ", (char *) dfault);
2.1       frystyk   139:     if (reply && msgnum>=0) {
                    140: #ifndef NO_STDIO
2.5       frystyk   141:         char buffer[200];
2.1       frystyk   142:        if (!fgets(buffer, 200, stdin)) return NO;
                    143:        buffer[strlen(buffer)-1] = '\0';                /* Overwrite newline */
                    144:        if (*buffer)
                    145:            HTAlert_setReplyMessage(reply, buffer);
                    146:        else if (dfault)
                    147:            HTAlert_setReplyMessage(reply, (char *) dfault);
                    148:        else
                    149:            return NO;
                    150:        return YES;
                    151: #endif
                    152:     }
                    153:     return NO;
                    154: }
                    155: 
                    156: /*     Prompt for password without echoing the reply. Reply text is
2.10      frystyk   157: **     either NULL on error or a dynamic string which the caller must HT_FREE.
2.1       frystyk   158: */
                    159: PUBLIC BOOL HTPromptPassword (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   160:                              int msgnum, const char * dfault, void * input,
2.1       frystyk   161:                              HTAlertPar * reply)
                    162: {
                    163:     if (reply && msgnum>=0) {
2.10      frystyk   164: #ifdef HAVE_GETPASS
2.1       frystyk   165:        char * pw = (char *) getpass(HTDialogs[msgnum]);
2.2       frystyk   166:        if (pw) HTAlert_setReplySecret(reply, pw);
2.1       frystyk   167:        return YES;
2.10      frystyk   168: #else
2.11      eric      169:        return NO;      /* needed for WWW_MSWINDOWS */
2.10      frystyk   170: #endif /* HAVE_GETPASS */
2.1       frystyk   171:     }
                    172:     return NO;
                    173: }
                    174: 
                    175: /*     Username and password
                    176: **     ---------------------
                    177: **     Prompt Username and password as an atomic operation
                    178: */
                    179: PUBLIC BOOL HTPromptUsernameAndPassword (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   180:                                         int msgnum, const char * dfault,
2.1       frystyk   181:                                         void * input, HTAlertPar * reply)
                    182: {
2.21      frystyk   183:     BOOL status = HTPrompt(request, op, msgnum, dfault, input, reply);
2.1       frystyk   184:     return status ?
                    185:        HTPromptPassword(request, op, HT_MSG_PW, dfault, input, reply) : NO;
                    186: }
                    187: 
                    188: /*     HTError_print
                    189: **     -------------
                    190: **     Default function that creates an error message using HTAlert() to
                    191: **     put out the contents of the error_stack messages. Furthermore, the
                    192: **     error_info structure contains a name of a help file that might be put
                    193: **     up as a link. This file can then be multi-linguistic.
                    194: */
                    195: PUBLIC BOOL HTError_print (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   196:                           int msgnum, const char * dfault, void * input,
2.1       frystyk   197:                           HTAlertPar * reply)
                    198: {
                    199:     HTList *cur = (HTList *) input;
                    200:     HTError *pres;
                    201:     HTErrorShow showmask = HTError_show();
                    202:     HTChunk *msg = NULL;
                    203:     int code;
2.8       eric      204:     if (WWWTRACE) HTTrace("HTError..... Generating message\n");
2.1       frystyk   205:     if (!request || !cur) return NO;
                    206:     while ((pres = (HTError *) HTList_nextObject(cur))) {
                    207:        int index = HTError_index(pres);
                    208:        if (HTError_doShow(pres)) {
                    209:            if (!msg) {
                    210:                HTSeverity severity = HTError_severity(pres);
2.3       frystyk   211:                msg = HTChunk_new(128);
2.1       frystyk   212:                if (severity == ERR_WARN)
2.3       frystyk   213:                    HTChunk_puts(msg, "Warning: ");
2.1       frystyk   214:                else if (severity == ERR_NON_FATAL)
2.3       frystyk   215:                    HTChunk_puts(msg, "Non Fatal Error: ");
2.1       frystyk   216:                else if (severity == ERR_FATAL)
2.3       frystyk   217:                    HTChunk_puts(msg, "Fatal Error: ");
2.1       frystyk   218:                else if (severity == ERR_INFO)
2.3       frystyk   219:                    HTChunk_puts(msg, "Information: ");
2.1       frystyk   220:                else {
                    221:                    if (WWWTRACE)
2.8       eric      222:                        HTTrace("HTError..... Unknown Classification of Error (%d)...\n", severity);
2.3       frystyk   223:                    HTChunk_delete(msg);
2.1       frystyk   224:                    return NO;
                    225:                }
                    226: 
                    227:                /* Error number */
                    228:                if ((code = HTErrors[index].code) > 0) {
                    229:                    char buf[10];
                    230:                    sprintf(buf, "%d ", code);
2.3       frystyk   231:                    HTChunk_puts(msg, buf);
2.1       frystyk   232:                }
                    233:            } else
2.3       frystyk   234:                HTChunk_puts(msg, "\nReason: ");
                    235:            HTChunk_puts(msg, HTErrors[index].msg);         /* Error message */
2.1       frystyk   236: 
                    237:            if (showmask & HT_ERR_SHOW_PARS) {           /* Error parameters */
                    238:                int length;
                    239:                int cnt;                
                    240:                char *pars = (char *) HTError_parameter(pres, &length);
                    241:                if (length && pars) {
2.3       frystyk   242:                    HTChunk_puts(msg, " (");
2.1       frystyk   243:                    for (cnt=0; cnt<length; cnt++) {
                    244:                        char ch = *(pars+cnt);
                    245:                        if (ch < 0x20 || ch >= 0x7F)
2.3       frystyk   246:                            HTChunk_putc(msg, '#');
2.1       frystyk   247:                        else
2.3       frystyk   248:                            HTChunk_putc(msg, ch);
2.1       frystyk   249:                    }
2.3       frystyk   250:                    HTChunk_puts(msg, ") ");
2.1       frystyk   251:                }
                    252:            }
                    253: 
                    254:            if (showmask & HT_ERR_SHOW_LOCATION) {         /* Error Location */
2.3       frystyk   255:                HTChunk_puts(msg, "This occured in ");
                    256:                HTChunk_puts(msg, HTError_location(pres));
                    257:                HTChunk_putc(msg, '\n');
2.1       frystyk   258:            }
                    259: 
                    260:            /*
                    261:            ** Make sure that we don't get this error more than once even
                    262:            ** if we are keeping the error stack from one request to another
                    263:            */
                    264:            HTError_setIgnore(pres);
                    265:            
                    266:            /* If we only are show the most recent entry then break here */
                    267:            if (showmask & HT_ERR_SHOW_FIRST)
                    268:                break;
                    269:        }
                    270:     }
                    271:     if (msg) {
2.3       frystyk   272:        HTChunk_putc(msg, '\n');
2.8       eric      273:        HTTrace("WARNING: %s\n", HTChunk_data(msg));
2.3       frystyk   274:        HTChunk_delete(msg);
2.1       frystyk   275:     }
                    276:     return YES;
                    277: }
                    278: 
2.6       frystyk   279: /*     HTError_response
                    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_response (HTRequest * request, HTAlertOpcode op,
2.10      frystyk   287:                              int msgnum, const char * dfault, void * input,
2.6       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 HTTP response\n");
2.6       frystyk   296:     if (!request || !cur || !reply) return NO;
                    297:     while ((pres = (HTError *) HTList_nextObject(cur))) {
                    298:        int index = HTError_index(pres);
                    299:        if (HTError_doShow(pres)) {
                    300:            if (!msg) {
                    301:                msg = HTChunk_new(128);
                    302:                if ((code = HTErrors[index].code) > 0) {
                    303:                    char * reason = HTErrors[index].msg;
2.7       frystyk   304:                    char * buf;
                    305:                    if ((buf = (char  *) HT_MALLOC(20 + strlen(reason))) == NULL)
                    306:                        HT_OUTOFMEM("HTError_response");
2.6       frystyk   307:                    sprintf(buf,"%s %d %s%c%c",HTTP_VERSION,code,reason,CR,LF);
                    308:                    HTAlert_assignReplyMessage(reply, buf);
                    309:                }
                    310:            } else {
                    311:                HTChunk_puts(msg, "\nReason: ");
                    312:                HTChunk_puts(msg, HTErrors[index].msg);     /* Error message */
                    313:            }
                    314: 
                    315:            if (showmask & HT_ERR_SHOW_PARS) {           /* Error parameters */
                    316:                int length;
                    317:                int cnt;                
                    318:                char *pars = (char *) HTError_parameter(pres, &length);
                    319:                if (length && pars) {
                    320:                    HTChunk_puts(msg, " (");
                    321:                    for (cnt=0; cnt<length; cnt++) {
                    322:                        char ch = *(pars+cnt);
                    323:                        if (ch < 0x20 || ch >= 0x7F)
                    324:                            HTChunk_putc(msg, '#');
                    325:                        else
                    326:                            HTChunk_putc(msg, ch);
                    327:                    }
                    328:                    HTChunk_puts(msg, ") ");
                    329:                }
                    330:            }
                    331: 
                    332:            if (showmask & HT_ERR_SHOW_LOCATION) {         /* Error Location */
                    333:                HTChunk_puts(msg, "This occured in ");
                    334:                HTChunk_puts(msg, HTError_location(pres));
                    335:                HTChunk_putc(msg, '\n');
                    336:            }
                    337: 
                    338:            /*
                    339:            ** Make sure that we don't get this error more than once even
                    340:            ** if we are keeping the error stack from one request to another
                    341:            */
                    342:            HTError_setIgnore(pres);
                    343:            
                    344:            /* If we only are show the most recent entry then break here */
                    345:            if (showmask & HT_ERR_SHOW_FIRST)
                    346:                break;
                    347:        }
                    348:     }
                    349:     if (msg) {
                    350:        HTChunk_putc(msg, '\n');
                    351: #if 0
2.8       eric      352:        HTTrace("WARNING: %s\n", HTChunk_data(msg));
2.6       frystyk   353: #endif
                    354:        HTChunk_delete(msg);
                    355:     }
                    356:     return YES;
                    357: }

Webmaster