HTDialog.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:13k
源码类别:

浏览器

开发平台:

Unix_Linux

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