📄 htwais.c
字号:
FREE(dbname); } PUTS(headline); END(HTML_A); FREE(docname); } else { PUTS(gettext("(bad doc id)")); } } sprintf(line, "%5ld %5ld ", head->Score, head->Lines); PUTS( line); MAYBE_END(HTML_LI); } /* next document header */ } /* if there were any document headers */ if (info->ShortHeaders != 0) { k = 0; while (info->ShortHeaders[k] != 0) { i++; PUTS( gettext("(Short Header record, can't display)")); } } if (info->LongHeaders != 0) { k = 0; while (info->LongHeaders[k] != 0) { i++; PUTS( gettext("\nLong Header record, can't display\n")); } } if (info->Text != 0) { k = 0; while (info->Text[k] != 0) { i++; PUTS( gettext("\nText record\n")); output_text_record((HTStream*)target, info->Text[k++], false, false); } } if (info->Headlines != 0) { k = 0; while (info->Headlines[k] != 0) { i++; PUTS( gettext("\nHeadline record, can't display\n")); /* dsply_headline_record( info->Headlines[k++]); */ } } if (info->Codes != 0) { k = 0; while (info->Codes[k] != 0) { i++; PUTS( gettext("\nCode record, can't display\n")); /* dsply_code_record( info->Codes[k++]); */ } } } /* Loop: display user info */ END(HTML_OL); PUTC('\n');}/* Load by name HTLoadWAIS** ============**** This renders any object or search as required.*/PUBLIC int HTLoadWAIS ARGS4( CONST char *, arg, HTParentAnchor *, anAnchor, HTFormat, format_out, HTStream*, sink)#define MAX_KEYWORDS_LENGTH 1000#define MAX_SERVER_LENGTH 1000#define MAX_DATABASE_LENGTH 1000#define MAX_SERVICE_LENGTH 1000#define MAXDOCS 200{ char * key; /* pointer to keywords in URL */ char* request_message = NULL; /* arbitrary message limit */ char* response_message = NULL; /* arbitrary message limit */ long request_buffer_length; /* how of the request is left */ SearchResponseAPDU *retrieval_response = 0; char keywords[MAX_KEYWORDS_LENGTH + 1]; char *server_name; char *wais_database = NULL; /* name of current database */ char *www_database; /* Same name escaped */ char *service; char *doctype; char *doclength; long document_length; char *docname;#ifdef VMS long connection = 0;#else FILE *connection = NULL;#endif /* VMS */ char * names; /* Copy of arg to be hacked up */ BOOL ok = NO; int return_status = HT_LOADED; int rv; if (!acceptable_inited) init_acceptable(); /* Decipher and check syntax of WWW address: ** ---------------------------------------- ** ** First we remove the "wais:" if it was specified. 920110 */ names = HTParse(arg, "", PARSE_HOST | PARSE_PATH | PARSE_PUNCTUATION); key = strchr(names, '?'); if (key) { char * p; *key++ = 0; /* Split off keywords */ for (p=key; *p; p++) if (*p == '+') *p = ' '; HTUnEscape(key); } if (names[0] == '/') { server_name = names+1; if ((as_gate =(*server_name == '/')) != 0) server_name++; /* Accept one or two */ www_database = strchr(server_name,'/'); if (www_database) { *www_database++ = 0; /* Separate database name */ doctype = strchr(www_database, '/'); if (key) ok = YES; /* Don't need doc details */ else if (doctype) { /* If not search parse doc details */ *doctype++ = 0; /* Separate rest of doc address */ doclength = strchr(doctype, '/'); if (doclength) { *doclength++ = 0; document_length = atol(doclength); if (document_length) { docname = strchr(doclength, '/'); if (docname) { *docname++ = 0; ok = YES; /* To avoid a goto! */ } /* if docname */ } /* if document_length valid */ } /* if doclength */ } else { /* no doctype? Assume index required */ if (!key) key = ""; ok = YES; } /* if doctype */ } /* if database */ } if (!ok) return HTLoadError(sink, 500, gettext("Syntax error in WAIS URL")); CTRACE((tfp, "HTWAIS: Parsed OK\n")); service = strchr(names, ':'); if (service) *service++ = 0; else service = "210"; if (server_name[0] == 0) {#ifdef VMS connection = 0;#else connection = NULL;#endif /* VMS */ } else if (!(key && !*key)) { int status; CTRACE((tfp, "===WAIS=== calling mosaic_connect_to_server\n")); status = mosaic_connect_to_server(server_name, atoi(service), &connection); if (status == 0) { CTRACE((tfp, "===WAIS=== connection failed\n")); FREE(names); return HT_NOT_LOADED; } else if (status == -1) { CTRACE((tfp, "===WAIS=== connection interrupted\n")); FREE(names); return HT_NOT_LOADED; } } StrAllocCopy(wais_database,www_database); HTUnEscape(wais_database); /* ** This below fixed size stuff is terrible. */#ifdef VMS if ((request_message = typecallocn(char, MAX_MESSAGE_LEN)) == 0) outofmem(__FILE__, "HTLoadWAIS"); if ((response_message = typecallocn(char, MAX_MESSAGE_LEN)) == 0) outofmem(__FILE__, "HTLoadWAIS");#else request_message = (char*)s_malloc((size_t)MAX_MESSAGE_LEN * sizeof(char)); response_message = (char*)s_malloc((size_t)MAX_MESSAGE_LEN * sizeof(char));#endif /* VMS */ /* ** If keyword search is performed but there are no keywords, ** the user has followed a link to the index itself. It would be ** appropriate at this point to send him the .SRC file - how? */ if (key && !*key) { /* I N D E X */#ifdef CACHE_FILE_PREFIX char * filename = NULL; FILE * fp;#endif HTStructured * target = HTML_new(anAnchor, format_out, sink); START(HTML_HEAD); PUTC('\n'); HTStartIsIndex(target, HTWAIS_SOLICIT_QUERY , NULL); PUTC('\n'); { START(HTML_TITLE); PUTS(wais_database); PUTS(gettext(" (WAIS Index)")); END(HTML_TITLE); PUTC('\n'); END(HTML_HEAD); PUTC('\n'); START(HTML_H1); PUTS(gettext("WAIS Index: ")); START(HTML_EM); PUTS(wais_database); END(HTML_EM); END(HTML_H1); PUTC('\n'); PUTS(gettext("This is a link for searching the ")); START(HTML_EM); PUTS(wais_database); END(HTML_EM); PUTS(gettext(" WAIS Index.\n")); } /* ** If we have seen a source file for this database, use that. */#ifdef CACHE_FILE_PREFIX HTSprintf0(&filename, "%sWSRC-%s:%s:%.100s.txt", CACHE_FILE_PREFIX, server_name, service, www_database); fp = fopen(filename, "r"); /* Have we found this already? */ CTRACE((tfp, "HTWAIS: Description of server %s %s.\n", filename, fp ? "exists already" : "does NOT exist!")); if (fp) { char c; START(HTML_PRE); /* Preformatted description */ PUTC('\n'); while((c=getc(fp))!=EOF) PUTC(c); /* Transfer file */ END(HTML_PRE); fclose(fp); } FREE(filename);#endif START(HTML_P); PUTS(gettext("\nEnter the 's'earch command and then specify search words.\n")); FREE_TARGET; } else if (key) { /* S E A R C H */ char *p; HTStructured * target; strncpy(keywords, key, MAX_KEYWORDS_LENGTH); while ((p = strchr(keywords, '+')) != 0) *p = ' '; /* ** Send advance title to get something fast to the other end. */ target = HTML_new(anAnchor, format_out, sink); START(HTML_HEAD); PUTC('\n'); HTStartIsIndex(target, HTWAIS_SOLICIT_QUERY, NULL); PUTC('\n'); START(HTML_TITLE); PUTS(keywords); PUTS(gettext(" (in ")); PUTS(wais_database); PUTC(')'); END(HTML_TITLE); PUTC('\n'); END(HTML_HEAD); PUTC('\n'); START(HTML_H1); PUTS(gettext("WAIS Search of \"")); START(HTML_EM); PUTS(keywords); END(HTML_EM); PUTS(gettext("\" in: ")); START(HTML_EM); PUTS(wais_database); END(HTML_EM); END(HTML_H1); PUTC('\n'); request_buffer_length = MAX_MESSAGE_LEN; /* Amount left */ CTRACE((tfp, "HTWAIS: Search for `%s' in `%s'\n", keywords, wais_database)); if(NULL == generate_search_apdu(request_message + HEADER_LENGTH, &request_buffer_length, keywords, wais_database, NULL, MAXDOCS)) {#ifdef VMS HTAlert (gettext("HTWAIS: Request too large.")); return_status = HT_NOT_LOADED; FREE_TARGET; goto CleanUp;#else panic("request too large");#endif /* VMS */ } HTProgress(gettext("Searching WAIS database...")); rv = interpret_message (request_message, MAX_MESSAGE_LEN - request_buffer_length, response_message, MAX_MESSAGE_LEN, connection, false /* true verbose */ ); if (rv == HT_INTERRUPTED) { HTAlert (gettext("Search interrupted.")); return_status = HT_INTERRUPTED; FREE_TARGET; goto CleanUp; } else if (!rv) {#ifdef VMS HTAlert (HTWAIS_MESSAGE_TOO_BIG); return_status = HT_NOT_LOADED; FREE_TARGET; goto CleanUp;#else panic("returned message too large");#endif /* VMS */ } else { /* returned message ok */ SearchResponseAPDU *query_response = 0; readSearchResponseAPDU(&query_response, response_message + HEADER_LENGTH); display_search_response(target, query_response, wais_database, keywords); if (query_response->DatabaseDiagnosticRecords) freeWAISSearchResponse( query_response->DatabaseDiagnosticRecords); freeSearchResponseAPDU( query_response); } /* returned message not too large */ FREE_TARGET; } else { /* D O C U M E N T F E T C H */ HTFormat format_in; boolean binary; /* how to transfer stuff coming over */ HTStream * target; long count; any doc_chunk; any * docid = &doc_chunk; CTRACE((tfp, "HTWAIS: Retrieve document id `%s' type `%s' length %ld\n", docname, doctype, document_length)); format_in = !strcmp(doctype, "WSRC") ? HTAtom_for("application/x-wais-source") : !strcmp(doctype, "TEXT") ? HTAtom_for("text/plain") : !strcmp(doctype, "HTML") ? HTAtom_for("text/html") : !strcmp(doctype, "GIF") ? HTAtom_for("image/gif") : HTAtom_for("application/octet-stream"); binary = 0 != strcmp(doctype, "WSRC") && 0 != strcmp(doctype, "TEXT") && 0 != strcmp(doctype, "HTML") ; target = HTStreamStack(format_in, format_out, sink, anAnchor); if (!target) return HTLoadError(sink, 500, gettext("Can't convert format of WAIS document")); /* ** Decode hex or litteral format for document ID. */ WAIS_from_WWW(docid, docname); /* ** Loop over slices of the document. */ for (count = 0; count * CHARS_PER_PAGE < document_length; count++) {#ifdef VMS char *type = NULL; StrAllocCopy(type, doctype);#else char *type = s_strdup(doctype); /* Gets freed I guess */#endif /* VMS */ request_buffer_length = MAX_MESSAGE_LEN; /* Amount left */ CTRACE((tfp, "HTWAIS: Slice number %ld\n", count)); if (HTCheckForInterrupt()) { HTAlert (TRANSFER_INTERRUPTED); (*target->isa->_abort)(target, NULL);#ifdef VMS FREE(type);#endif /* VMS */ return_status = HT_NOT_LOADED; goto CleanUp; } if (0 == generate_retrieval_apdu(request_message + HEADER_LENGTH, &request_buffer_length, docid, CT_byte, count * CHARS_PER_PAGE, ((count + 1) * CHARS_PER_PAGE <= document_length ? (count + 1) * CHARS_PER_PAGE : document_length), type, wais_database)) {#ifdef VMS HTAlert (gettext("HTWAIS: Request too long.")); return_status = HT_NOT_LOADED; FREE_TARGET; FREE(type); FREE(docid->bytes); goto CleanUp;#else panic("request too long");#endif /* VMS */ } /* ** Actually do the transaction given by request_message. */ HTProgress(gettext("Fetching WAIS document...")); rv = interpret_message(request_message, MAX_MESSAGE_LEN - request_buffer_length, response_message, MAX_MESSAGE_LEN, connection, false /* true verbose */ ); if (rv == HT_INTERRUPTED) { HTAlert (TRANSFER_INTERRUPTED); return_status = HT_INTERRUPTED; FREE_TARGET; FREE(type); FREE(docid->bytes); goto CleanUp; } else if (!rv) {#ifdef VMS HTAlert (HTWAIS_MESSAGE_TOO_BIG); return_status = HT_NOT_LOADED; FREE_TARGET; FREE(type); FREE(docid->bytes); goto CleanUp;#else panic("Returned message too large");#endif /* VMS */ } /* ** Parse the result which came back into memory. */ readSearchResponseAPDU(&retrieval_response, response_message + HEADER_LENGTH); if (NULL == ((WAISSearchResponse *) retrieval_response->DatabaseDiagnosticRecords)->Text) { /* display_search_response(target, retrieval_response, wais_database, keywords); */ PUTS(gettext("No text was returned!\n")); /* panic("No text was returned"); */ } else { output_text_record(target, ((WAISSearchResponse *) retrieval_response->DatabaseDiagnosticRecords)->Text[0], false, binary); } /* If text existed */#ifdef VMS FREE(type);#endif /* VMS */ } /* Loop over slices */ FREE_TARGET; FREE(docid->bytes); freeWAISSearchResponse( retrieval_response->DatabaseDiagnosticRecords); freeSearchResponseAPDU( retrieval_response); } /* If document rather than search */CleanUp: /* ** (This postponed until later, after a timeout:) */#ifdef VMS if (connection) NETCLOSE((int)connection);#else if (connection) fclose(connection);#endif /* VMS */ FREE(wais_database);#ifdef VMS FREE(request_message); FREE(response_message);#else s_free(request_message); s_free(response_message);#endif /* VMS */ FREE(names); return (return_status);}#ifdef GLOBALDEF_IS_MACRO#define _HTWAIS_C_1_INIT { "wais", HTLoadWAIS, NULL }GLOBALDEF(HTProtocol, HTWAIS, _HTWAIS_C_1_INIT);#elseGLOBALDEF PUBLIC HTProtocol HTWAIS = { "wais", HTLoadWAIS, NULL };#endif /* GLOBALDEF_IS_MACRO */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -