📄 htwais.c
字号:
/* Accept one or two */ if ((theWAISinfo->as_gate = (*server_name == '/'))) server_name++; if ((theWAISinfo->www_database = strchr(server_name, '/'))) { *(theWAISinfo->www_database)++ = 0; /* Separate database name */ doctype = strchr(theWAISinfo->www_database, '/'); if (key) ok = YES; /* Don't need doc details */ else if (doctype) { /* If not search parse doc details */ char* doclength; *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) { char *unescaped = NULL; StrAllocCopy(unescaped, arg); HTUnEscape(unescaped); HTRequest_addError(request, ERR_FATAL, NO, HTERR_BAD_REQUEST, (void *) unescaped, (int) strlen(unescaped), "HTLoadWAIS"); HT_FREE(unescaped); HT_FREE(theWAISinfo->names); return -1; } HTTRACE(PROT_TRACE, "HTLoadWAIS.. URL Parsed OK\n"); theWAISinfo->state = HTWAIS_NEED_CONNECTION; if ((service = strchr(theWAISinfo->names, ':'))) *service++ = 0; else service = "210"; if ((server_name ? server_name[0] : 0)) { boolean do_need_to_connect_to_server = true; if (key) if ((*key) == 0) do_need_to_connect_to_server = false; if (do_need_to_connect_to_server) { if ((theWAISinfo->connection = connect_to_server(server_name, atoi(service))) == NULL) { char* host = HTParse(arg, "", PARSE_HOST); HTTRACE(PROT_TRACE, "HTLoadWAIS.." " Can't open connection to %s via service %s.\n" _ server_name _ service); HTRequest_addError(request, ERR_FATAL, NO, HTERR_WAIS_NO_CONNECT, (void *) host, (int) strlen(host), "HTLoadWAIS"); theWAISinfo->result = HT_ERROR; HTWAISCleanup(request, status); return status; } } } else theWAISinfo->connection = NULL; StrAllocCopy(theWAISinfo->wais_database, theWAISinfo->www_database); HTUnEscape(theWAISinfo->wais_database); /* Make title name without the .src */ { char *srcstr; StrAllocCopy(theWAISinfo->basetitle, theWAISinfo->wais_database); if ((srcstr = strstr(theWAISinfo->basetitle, ".src")) != NULL) *srcstr = '\0'; } /* This below fixed size stuff is terrible */ if ((theWAISinfo->request_message = (char*)s_malloc((size_t)MAX_MESSAGE_LEN * sizeof(char))) == NULL) HT_OUTOFMEM("WAIS request message"); if ((theWAISinfo->response_message = (char*)s_malloc((size_t)MAX_MESSAGE_LEN * sizeof(char))) == NULL) HT_OUTOFMEM("WAIS response message"); /* 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) { if (*key) { /* * S E A R C H (nonempty key (*key != 0)) */ char *p; HTStructured* target; theWAISinfo->state = HTWAIS_NEED_RESPONSE; strncpy(keywords, key, MAX_KEYWORDS_LENGTH); while ((p = strchr(keywords,'+'))) *p = ' '; /* Send advance title to get something fast to the other end */ target = HTMLGenerator(request, NULL, WWW_HTML, format_out, sink); START(HTML_HTML); START(HTML_HEAD); START(HTML_TITLE); PUTS(keywords); PUTS(" in "); PUTS(theWAISinfo->basetitle); END(HTML_TITLE); END(HTML_HEAD); START(HTML_BODY); START(HTML_H1); PUTS("WAIS Search of \""); PUTS(keywords); PUTS("\" in "); PUTS(theWAISinfo->basetitle); END(HTML_H1); START(HTML_ISINDEX); request_buffer_length = MAX_MESSAGE_LEN; /* Amount left */ HTTRACE(PROT_TRACE, "HTLoadWAIS.. Search for `%s' in `%s'\n" _ keywords _ theWAISinfo->wais_database); if (generate_search_apdu(theWAISinfo->request_message + HEADER_LENGTH, &request_buffer_length, keywords, theWAISinfo->wais_database, NULL, HTMaxWAISLines) == NULL) { HTTRACE(PROT_TRACE, "WAIS Search. Too many lines in response\n"); HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_OVERFLOW, NULL, 0, "HTLoadWAIS"); } if (!interpret_message(theWAISinfo->request_message, MAX_MESSAGE_LEN - request_buffer_length, theWAISinfo->response_message, MAX_MESSAGE_LEN, theWAISinfo->connection, false /* true verbose */ )) { HTTRACE(PROT_TRACE, "WAIS Search. Too many lines in response\n"); HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_OVERFLOW, NULL, 0, "HTLoadWAIS"); } else { /* returned message ok */ SearchResponseAPDU *query_response = 0; readSearchResponseAPDU(&query_response, theWAISinfo->response_message + HEADER_LENGTH); display_search_response(target, query_response, theWAISinfo->wais_database, keywords); if (query_response->DatabaseDiagnosticRecords) freeWAISSearchResponse(query_response->DatabaseDiagnosticRecords); freeSearchResponseAPDU(query_response); } /* returned message not too large */ END(HTML_BODY); END(HTML_HTML); FREE_TARGET; HTAnchor_setFormat(anchor, WWW_HTML); theWAISinfo->result = status = HT_LOADED; } else { /* * I N D E X (key is empty (*key = 0)) */#ifdef CACHE_FILE_PREFIX char filename[256]; FILE * fp;#endif HTStructured* target = HTMLGenerator(request, NULL, WWW_HTML, format_out, sink); theWAISinfo->state = HTWAIS_NEED_REQUEST; { START(HTML_HTML); START(HTML_HEAD); START(HTML_TITLE); PUTS(theWAISinfo->basetitle); PUTS(" Index"); END(HTML_TITLE); END(HTML_HEAD); START(HTML_BODY); START(HTML_H1); PUTS("WAIS Index: "); PUTS(theWAISinfo->basetitle); END(HTML_H1); } START(HTML_ISINDEX); /* If we have seen a source file for this database, use that: */#ifdef CACHE_FILE_PREFIX sprintf(filename, "%sWSRC-%s:%s:%.100s.txt", CACHE_FILE_PREFIX, server_name, service, theWAISinfo->www_database); fp = fopen(filename, "r"); /* Have we found this already? */ HTTRACE(PROT_TRACE, "HTLoadWAIS.. Description of server %s %s.\n" _ filename _ fp ? "exists already" : "does NOT exist!"); if (fp) { int c; START(HTML_PRE); /* Preformatted description */ while((c=getc(fp)) != EOF) PUTC(c); /* Transfer file */ END(HTML_PRE); fclose(fp); }#endif END(HTML_BODY); END(HTML_HTML); FREE_TARGET; } HTAnchor_setFormat(anchor, WWW_HTML); theWAISinfo->result = status = HT_LOADED; } else {/* document rather than search */ /* * D O C U M E N T F E T C H (no key (key == NULL)) */ boolean binary = true; /* how to transfer stuff coming over */ HTStream* target; HTAtom* document_type_atom = HTAtom_for("application/octet-stream"); long count; any doc_chunk; any * docid = &doc_chunk; theWAISinfo->state = HTWAIS_FETCH_DOCUMENT; HTTRACE(PROT_TRACE, "HTLoadWAIS.. Retrieve document `%s'\n" "............ type `%s' length %ld\n" _ (docname ? docname : "unknown") _ (doctype ? doctype : "unknown") _ document_length); if (doctype) { if (strcmp(doctype, "WSRC") == 0) { document_type_atom = HTAtom_for("application/x-wais-source"); binary = false; } else if (strcmp(doctype, "TEXT") == 0) { document_type_atom = WWW_UNKNOWN; binary = false; } else if (strcmp(doctype, "HTML") == 0) { document_type_atom = WWW_HTML; binary = false; } else if (strcmp(doctype, "GIF") == 0) document_type_atom = WWW_GIF; } HTAnchor_setFormat(anchor, document_type_atom); /* Guess on TEXT format as it might be HTML */ if ((target = HTStreamStack(HTAnchor_format(anchor), HTRequest_outputFormat(request), HTRequest_outputStream(request), request, YES)) == NULL) { theWAISinfo->result = HT_ERROR; status = -1; HTWAISCleanup(request, status); return status; } /* 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++) { char *type = s_strdup(doctype); request_buffer_length = MAX_MESSAGE_LEN; /* Amount left */ HTTRACE(PROT_TRACE, "HTLoadWAIS.. Slice number %ld\n" _ count); if (generate_retrieval_apdu(theWAISinfo->request_message + HEADER_LENGTH, &request_buffer_length, docid, CT_byte, count * CHARS_PER_PAGE, HTMIN((count + 1) * CHARS_PER_PAGE, document_length), type, theWAISinfo->wais_database) == 0) { HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_OVERFLOW, NULL, 0, "HTLoadWAIS"); } HT_FREE(type); /* Actually do the transaction given by request_message */ if (interpret_message(theWAISinfo->request_message, MAX_MESSAGE_LEN - request_buffer_length, theWAISinfo->response_message, MAX_MESSAGE_LEN, theWAISinfo->connection, false /* true verbose */ ) == 0) { HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_OVERFLOW, NULL, 0, "HTLoadWAIS"); } /* Parse the result which came back into memory. */ readSearchResponseAPDU(&retrieval_response, theWAISinfo->response_message + HEADER_LENGTH); { WAISSearchResponse* searchres = (WAISSearchResponse*)retrieval_response->DatabaseDiagnosticRecords; if (!searchres->Text) { if (searchres->Diagnostics && *searchres->Diagnostics && (*searchres->Diagnostics)->ADDINFO) { char *errmsg = (*searchres->Diagnostics)->ADDINFO; HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_MODULE, (void *) errmsg, (int) strlen(errmsg), "HTLoadWAIS"); } else { HTRequest_addError(request, ERR_WARN, NO, HTERR_WAIS_MODULE, NULL, 0, "HTLoadWAIS"); } (*target->isa->_free)(target); HTRequest_setOutputStream(request, NULL); HT_FREE(docid->bytes); freeWAISSearchResponse(retrieval_response->DatabaseDiagnosticRecords); freeSearchResponseAPDU(retrieval_response); theWAISinfo->result = HT_OK; HTWAISCleanup(request, status); } else { output_text_record(target, *searchres->Text, false, binary); freeWAISSearchResponse(retrieval_response->DatabaseDiagnosticRecords); freeSearchResponseAPDU(retrieval_response); } /* If text existed */ } } /* Loop over slices */ (*target->isa->_free)(target); HTRequest_setOutputStream(request, NULL); HT_FREE(docid->bytes); theWAISinfo->result = status = HT_LOADED; } /* End ``if (key)'' */ HTWAISCleanup(request, status); return status;}PUBLIC int HTLoadWAIS (SOCKET soc, HTRequest* request){ wais_info* theWAISinfo; /* Specific protocol information */ HTNet* net; /* Generic protocol information */ HTParentAnchor* anchor; /* * Initiate a new wais structure and bind to request structure. * This is actually state HTWAIS_BEGIN, * but it can't be in the state machine, * as we need the structure first. */ if (request) { if ((anchor = HTRequest_anchor(request)) == NULL) return HT_ERROR; if ((net = HTRequest_net(request)) == NULL) return HT_ERROR; } else return HT_ERROR; HTTRACE(PROT_TRACE, "HTWAIS...... Looking for `%s\'\n" _ HTAnchor_physical(anchor)); /* Get existing copy */ if ((theWAISinfo = (wais_info*)HTNet_context(net)) == NULL) { if ((theWAISinfo = (wais_info*)HT_CALLOC(1, sizeof(wais_info))) == NULL) HT_OUTOFMEM("HTLoadWAIS"); HTNet_setEventCallback(net, HTWAISEvent); HTNet_setEventParam(net, theWAISinfo); /* callbacks get theWAISinfo* */ HTNet_setContext(net, theWAISinfo); theWAISinfo->state = HTWAIS_BEGIN; theWAISinfo->result = HT_ERROR; theWAISinfo->net = net; } /* get it started - ops is ignored */ return HTWAISEvent(soc, theWAISinfo, HTEvent_BEGIN);}#endif /* HT_DIRECT_WAIS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -