| 1 |
/* |
| 2 |
* $LynxId: HTGopher.c,v 1.80 2025/01/07 15:25:47 tom Exp $ |
| 3 |
* |
| 4 |
* GOPHER ACCESS HTGopher.c |
| 5 |
* ============= |
| 6 |
* |
| 7 |
* History: |
| 8 |
* 26 Sep 90 Adapted from other accesses (News, HTTP) TBL |
| 9 |
* 29 Nov 91 Downgraded to C, for portable implementation. |
| 10 |
* 10 Mar 96 Foteos Macrides ([email protected]). Added a |
| 11 |
* form-based CSO/PH gateway. Can be invoked via a |
| 12 |
* "cso://host[:port]/" or "gopher://host:105/2" |
| 13 |
* URL. If a gopher URL is used with a query token |
| 14 |
* ('?'), the old ISINDEX procedure will be used |
| 15 |
* instead of the form-based gateway. |
| 16 |
* 15 Mar 96 Foteos Macrides ([email protected]). Pass |
| 17 |
* port 79, gtype 0 gopher URLs to the finger |
| 18 |
* gateway. |
| 19 |
*/ |
| 20 |
|
| 21 |
#define HTSTREAM_INTERNAL 1 |
| 22 |
|
| 23 |
#include <HTUtils.h> /* Coding convention macros */ |
| 24 |
#include <HTFile.h> /* For HTFileFormat() */ |
| 25 |
|
| 26 |
#ifndef DISABLE_GOPHER |
| 27 |
#include <HTAlert.h> |
| 28 |
#include <HTParse.h> |
| 29 |
#include <HTTCP.h> |
| 30 |
#include <HTFinger.h> |
| 31 |
#include <LYGlobalDefs.h> |
| 32 |
|
| 33 |
/* |
| 34 |
* Implements. |
| 35 |
*/ |
| 36 |
#include <HTGopher.h> |
| 37 |
|
| 38 |
#define GOPHER_PORT 70 /* See protocol spec */ |
| 39 |
#define CSO_PORT 105 /* See protocol spec */ |
| 40 |
#define BIG 1024 /* Bug */ |
| 41 |
#define LINE_LENGTH 256 /* Bug */ |
| 42 |
|
| 43 |
/* |
| 44 |
* Gopher entity types. |
| 45 |
*/ |
| 46 |
#define GOPHER_TEXT '0' |
| 47 |
#define GOPHER_MENU '1' |
| 48 |
#define GOPHER_CSO '2' |
| 49 |
#define GOPHER_ERROR '3' |
| 50 |
#define GOPHER_MACBINHEX '4' |
| 51 |
#define GOPHER_PCBINARY '5' |
| 52 |
#define GOPHER_UUENCODED '6' |
| 53 |
#define GOPHER_INDEX '7' |
| 54 |
#define GOPHER_TELNET '8' |
| 55 |
#define GOPHER_BINARY '9' |
| 56 |
#define GOPHER_GIF 'g' |
| 57 |
#define GOPHER_HTML 'h' /* HTML */ |
| 58 |
#define GOPHER_CHTML 'H' /* HTML */ |
| 59 |
#define GOPHER_SOUND 's' |
| 60 |
#define GOPHER_WWW 'w' /* W3 address */ |
| 61 |
#define GOPHER_IMAGE 'I' |
| 62 |
#define GOPHER_TN3270 'T' |
| 63 |
#define GOPHER_INFO 'i' |
| 64 |
#define GOPHER_DUPLICATE '+' |
| 65 |
#define GOPHER_PLUS_IMAGE ':' /* Addition from Gopher Plus */ |
| 66 |
#define GOPHER_PLUS_MOVIE ';' |
| 67 |
#define GOPHER_PLUS_SOUND '<' |
| 68 |
#define GOPHER_PLUS_PDF 'P' |
| 69 |
|
| 70 |
#include <HTFormat.h> |
| 71 |
|
| 72 |
/* |
| 73 |
* Hypertext object building machinery. |
| 74 |
*/ |
| 75 |
#include <HTML.h> |
| 76 |
|
| 77 |
#include <LYStrings.h> |
| 78 |
#include <LYUtils.h> |
| 79 |
#include <LYLeaks.h> |
| 80 |
|
| 81 |
#define PUTC(c) (*targetClass.put_character)(target, c) |
| 82 |
#define PUTS(s) (*targetClass.put_string)(target, s) |
| 83 |
#define START(e) (*targetClass.start_element)(target, e, NULL, NULL, -1, NULL) |
| 84 |
#define END(e) (*targetClass.end_element)(target, e, NULL) |
| 85 |
#define FREE_TARGET (*targetClass._free)(target) |
| 86 |
|
| 87 |
#define NEXT_CHAR HTGetCharacter() |
| 88 |
|
| 89 |
/* |
| 90 |
* Module-wide variables. |
| 91 |
*/ |
| 92 |
static int s; /* Socket for gopher or CSO host */ |
| 93 |
|
| 94 |
struct _HTStructured { |
| 95 |
const HTStructuredClass *isa; /* For gopher streams */ |
| 96 |
/* ... */ |
| 97 |
}; |
| 98 |
|
| 99 |
static HTStructured *target; /* the new gopher hypertext */ |
| 100 |
static HTStructuredClass targetClass; /* Its action routines */ |
| 101 |
|
| 102 |
struct _HTStream { |
| 103 |
HTStreamClass *isa; /* For form-based CSO gateway - FM */ |
| 104 |
}; |
| 105 |
|
| 106 |
typedef struct _CSOfield_info { /* For form-based CSO gateway - FM */ |
| 107 |
struct _CSOfield_info *next; |
| 108 |
char *name; |
| 109 |
char *attributes; |
| 110 |
char *description; |
| 111 |
int id; |
| 112 |
int lookup; |
| 113 |
int indexed; |
| 114 |
int url; |
| 115 |
int max_size; |
| 116 |
int defreturn; |
| 117 |
int explicit_return; |
| 118 |
int reserved; |
| 119 |
int gpublic; |
| 120 |
char name_buf[16]; /* Avoid malloc if we can */ |
| 121 |
char desc_buf[32]; /* Avoid malloc if we can */ |
| 122 |
char attr_buf[80]; /* Avoid malloc if we can */ |
| 123 |
} CSOfield_info; |
| 124 |
|
| 125 |
static CSOfield_info *CSOfields = NULL; /* For form-based CSO gateway - FM */ |
| 126 |
|
| 127 |
typedef struct _CSOformgen_context { /* For form-based CSO gateway - FM */ |
| 128 |
const char *host; |
| 129 |
const char *seek; |
| 130 |
CSOfield_info *fld; |
| 131 |
int port; |
| 132 |
int cur_line; |
| 133 |
int cur_off; |
| 134 |
int rep_line; |
| 135 |
int rep_off; |
| 136 |
int public_override; |
| 137 |
int field_select; |
| 138 |
} CSOformgen_context; |
| 139 |
|
| 140 |
/* Matrix of allowed characters in filenames |
| 141 |
* ========================================= |
| 142 |
*/ |
| 143 |
static BOOL acceptable_html[256]; |
| 144 |
static BOOL acceptable_file[256]; |
| 145 |
static BOOL acceptable_inited = NO; |
| 146 |
|
| 147 |
static void init_acceptable(void) |
| 148 |
{ |
| 149 |
unsigned int i; |
| 150 |
const char *good = |
| 151 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./-_$"; |
| 152 |
|
| 153 |
for (i = 0; i < 256; i++) { |
| 154 |
acceptable_html[i] = NO; |
| 155 |
acceptable_file[i] = NO; |
| 156 |
} |
| 157 |
for (; *good; good++) { |
| 158 |
acceptable_html[(unsigned int) *good] = YES; |
| 159 |
acceptable_file[(unsigned int) *good] = YES; |
| 160 |
} |
| 161 |
for (good = ";?=#"; *good; ++good) { |
| 162 |
acceptable_html[(unsigned int) *good] = YES; |
| 163 |
} |
| 164 |
acceptable_inited = YES; |
| 165 |
} |
| 166 |
|
| 167 |
/* Decode one hex character |
| 168 |
* ======================== |
| 169 |
*/ |
| 170 |
static const char hex[17] = "0123456789abcdef"; |
| 171 |
|
| 172 |
static char from_hex(int c) |
| 173 |
{ |
| 174 |
return (char) ((c >= '0') && (c <= '9') ? c - '0' |
| 175 |
: (c >= 'A') && (c <= 'F') ? c - 'A' + 10 |
| 176 |
: (c >= 'a') && (c <= 'f') ? c - 'a' + 10 |
| 177 |
: 0); |
| 178 |
} |
| 179 |
|
| 180 |
/* Paste in an Anchor |
| 181 |
* ================== |
| 182 |
* |
| 183 |
* The title of the destination is set, as there is no way |
| 184 |
* of knowing what the title is when we arrive. |
| 185 |
* |
| 186 |
* On entry, |
| 187 |
* HT is in append mode. |
| 188 |
* text points to the text to be put into the file, 0 terminated. |
| 189 |
* addr points to the hypertext reference address 0 terminated. |
| 190 |
*/ |
| 191 |
BOOLEAN HT_Is_Gopher_URL = FALSE; |
| 192 |
|
| 193 |
static void write_anchor(const char *text, const char *addr) |
| 194 |
{ |
| 195 |
BOOL present[HTML_A_ATTRIBUTES]; |
| 196 |
const char *value[HTML_A_ATTRIBUTES]; |
| 197 |
|
| 198 |
int i; |
| 199 |
|
| 200 |
for (i = 0; i < HTML_A_ATTRIBUTES; i++) |
| 201 |
present[i] = 0; |
| 202 |
present[HTML_A_HREF] = YES; |
| 203 |
((const char **) value)[HTML_A_HREF] = addr; |
| 204 |
present[HTML_A_TITLE] = YES; |
| 205 |
((const char **) value)[HTML_A_TITLE] = text; |
| 206 |
|
| 207 |
CTRACE((tfp, "HTGopher: adding URL: %s\n", addr)); |
| 208 |
|
| 209 |
HT_Is_Gopher_URL = TRUE; /* tell HTML.c that this is a Gopher URL */ |
| 210 |
(*targetClass.start_element) (target, HTML_A, present, |
| 211 |
(const char **) value, -1, NULL); |
| 212 |
|
| 213 |
PUTS(text); |
| 214 |
END(HTML_A); |
| 215 |
} |
| 216 |
|
| 217 |
/* Parse a Gopher Menu document |
| 218 |
* ============================ |
| 219 |
*/ |
| 220 |
static void parse_menu(const char *arg GCC_UNUSED, |
| 221 |
HTParentAnchor *anAnchor) |
| 222 |
{ |
| 223 |
char gtype; |
| 224 |
char this_type; |
| 225 |
int ich; |
| 226 |
char line[BIG]; |
| 227 |
char *name = NULL, *selector = NULL; /* Gopher menu fields */ |
| 228 |
char *host = NULL; |
| 229 |
char *port; |
| 230 |
char *p = line; |
| 231 |
const char *title; |
| 232 |
int bytes = 0; |
| 233 |
int BytesReported = 0; |
| 234 |
char buffer[128]; |
| 235 |
BOOL *valid_chars; |
| 236 |
|
| 237 |
#define TAB '\t' |
| 238 |
#define HEX_ESCAPE '%' |
| 239 |
|
| 240 |
START(HTML_HTML); |
| 241 |
PUTC('\n'); |
| 242 |
START(HTML_HEAD); |
| 243 |
PUTC('\n'); |
| 244 |
START(HTML_TITLE); |
| 245 |
if ((title = HTAnchor_title(anAnchor))) |
| 246 |
PUTS(title); |
| 247 |
else |
| 248 |
PUTS(GOPHER_MENU_TITLE); |
| 249 |
END(HTML_TITLE); |
| 250 |
PUTC('\n'); |
| 251 |
END(HTML_HEAD); |
| 252 |
PUTC('\n'); |
| 253 |
|
| 254 |
START(HTML_BODY); |
| 255 |
PUTC('\n'); |
| 256 |
START(HTML_H1); |
| 257 |
if ((title = HTAnchor_title(anAnchor))) |
| 258 |
PUTS(title); |
| 259 |
else |
| 260 |
PUTS(GOPHER_MENU_TITLE); |
| 261 |
END(HTML_H1); |
| 262 |
PUTC('\n'); |
| 263 |
START(HTML_PRE); |
| 264 |
PUTC('\n'); /* newline after HTML_PRE forces split-line */ |
| 265 |
this_type = GOPHER_ERROR; |
| 266 |
while ((ich = NEXT_CHAR) != EOF) { |
| 267 |
|
| 268 |
if (interrupted_in_htgetcharacter) { |
| 269 |
CTRACE((tfp, |
| 270 |
"HTGopher: Interrupted in HTGetCharacter, apparently.\n")); |
| 271 |
goto end_html; |
| 272 |
} |
| 273 |
|
| 274 |
if ((char) ich != LF) { |
| 275 |
const char *ss = NULL; |
| 276 |
|
| 277 |
/* |
| 278 |
* Help the -source output to look like the HTML equivalent of the |
| 279 |
* Gopher menu. |
| 280 |
*/ |
| 281 |
if (dump_output_immediately |
| 282 |
&& HTOutputFormat == WWW_DUMP) { |
| 283 |
if (ich == '<') { |
| 284 |
ss = "<"; |
| 285 |
} else if (ich == '>') { |
| 286 |
ss = ">"; |
| 287 |
} else if (ich == '&') { |
| 288 |
ss = "&"; |
| 289 |
} |
| 290 |
} |
| 291 |
if (ss != NULL) { |
| 292 |
if ((p + 5) < &line[BIG - 1]) { |
| 293 |
while (*ss != '\0') { |
| 294 |
*p++ = *ss++; |
| 295 |
} |
| 296 |
} |
| 297 |
} else { |
| 298 |
*p = (char) ich; /* Put character in line */ |
| 299 |
if (p < &line[BIG - 1]) |
| 300 |
p++; |
| 301 |
} |
| 302 |
|
| 303 |
} else { |
| 304 |
*p++ = '\0'; /* Terminate line */ |
| 305 |
bytes += (int) (p - line); /* add size */ |
| 306 |
p = line; /* Scan it to parse it */ |
| 307 |
port = NULL; /* Flag "not parsed" */ |
| 308 |
CTRACE((tfp, "HTGopher: Menu item: %s\n", line)); |
| 309 |
gtype = *p++; |
| 310 |
|
| 311 |
if (bytes > BytesReported + 1024) { |
| 312 |
sprintf(buffer, TRANSFERRED_X_BYTES, bytes); |
| 313 |
HTProgress(buffer); |
| 314 |
BytesReported = bytes; |
| 315 |
} |
| 316 |
|
| 317 |
/* Break on line with a dot by itself */ |
| 318 |
if ((gtype == '.') && ((*p == '\r') || (*p == 0))) |
| 319 |
break; |
| 320 |
|
| 321 |
if (gtype && *p) { |
| 322 |
name = p; |
| 323 |
selector = StrChr(name, TAB); |
| 324 |
if (selector) { |
| 325 |
*selector++ = '\0'; /* Terminate name */ |
| 326 |
/* |
| 327 |
* Gopher+ Type=0+ objects can be binary, and will |
| 328 |
* have 9 or 5 beginning their selector. Make sure |
| 329 |
* we don't trash the terminal by treating them as |
| 330 |
* text. - FM |
| 331 |
*/ |
| 332 |
if (gtype == GOPHER_TEXT && (*selector == GOPHER_BINARY || |
| 333 |
*selector == GOPHER_PCBINARY)) |
| 334 |
gtype = *selector; |
| 335 |
host = StrChr(selector, TAB); |
| 336 |
if (host) { |
| 337 |
*host++ = '\0'; /* Terminate selector */ |
| 338 |
port = StrChr(host, TAB); |
| 339 |
if (port) { |
| 340 |
char *junk; |
| 341 |
|
| 342 |
port[0] = ':'; /* delimit host a la W3 */ |
| 343 |
junk = StrChr(port, TAB); |
| 344 |
if (junk) |
| 345 |
*junk = '\0'; /* Chop port */ |
| 346 |
if ((port[1] == '0') && (!port[2])) |
| 347 |
port[0] = '\0'; /* 0 means none */ |
| 348 |
} /* no port */ |
| 349 |
} /* host ok */ |
| 350 |
} /* selector ok */ |
| 351 |
} |
| 352 |
/* gtype and name ok */ |
| 353 |
/* Nameless files are a separator line */ |
| 354 |
if (name != NULL && gtype == GOPHER_TEXT) { |
| 355 |
int i = (int) strlen(name) - 1; |
| 356 |
|
| 357 |
while (name[i] == ' ' && i >= 0) |
| 358 |
name[i--] = '\0'; |
| 359 |
if (i < 0) |
| 360 |
gtype = GOPHER_INFO; |
| 361 |
} |
| 362 |
|
| 363 |
if (gtype == GOPHER_WWW) { /* Gopher pointer to W3 */ |
| 364 |
PUTS("(HTML) "); |
| 365 |
write_anchor(name, selector); |
| 366 |
|
| 367 |
} else if (gtype == GOPHER_INFO) { |
| 368 |
/* Information or separator line */ |
| 369 |
PUTS(" "); |
| 370 |
PUTS(name); |
| 371 |
|
| 372 |
} else if (port && /* Other types need port */ |
| 373 |
(gtype != GOPHER_DUPLICATE || |
| 374 |
this_type != GOPHER_ERROR)) { |
| 375 |
char *address = NULL; |
| 376 |
|
| 377 |
if (gtype == GOPHER_TELNET) { |
| 378 |
PUTS(" (TEL) "); |
| 379 |
if (*selector == '/') |
| 380 |
++selector; |
| 381 |
HTSprintf0(&address, "%s//%s%s%s/", STR_TELNET_URL, |
| 382 |
selector, (*selector ? "@" : ""), host); |
| 383 |
} else if (gtype == GOPHER_TN3270) { |
| 384 |
PUTS("(3270) "); |
| 385 |
if (*selector == '/') |
| 386 |
++selector; |
| 387 |
HTSprintf0(&address, "%s//%s%s%s/", STR_TN3270_URL, |
| 388 |
selector, (*selector ? "@" : ""), host); |
| 389 |
} else { /* If parsed ok */ |
| 390 |
char *r; |
| 391 |
|
| 392 |
switch (gtype) { |
| 393 |
case GOPHER_TEXT: |
| 394 |
PUTS("(FILE) "); |
| 395 |
break; |
| 396 |
case GOPHER_MENU: |
| 397 |
PUTS(" (DIR) "); |
| 398 |
break; |
| 399 |
case GOPHER_DUPLICATE: |
| 400 |
PUTS(" (+++) "); |
| 401 |
break; |
| 402 |
case GOPHER_CSO: |
| 403 |
PUTS(" (CSO) "); |
| 404 |
break; |
| 405 |
case GOPHER_PCBINARY: |
| 406 |
PUTS(" (BIN) "); |
| 407 |
break; |
| 408 |
case GOPHER_UUENCODED: |
| 409 |
PUTS(" (UUE) "); |
| 410 |
break; |
| 411 |
case GOPHER_INDEX: |
| 412 |
PUTS(" (?) "); |
| 413 |
break; |
| 414 |
case GOPHER_BINARY: |
| 415 |
PUTS(" (BIN) "); |
| 416 |
break; |
| 417 |
case GOPHER_GIF: |
| 418 |
case GOPHER_IMAGE: |
| 419 |
case GOPHER_PLUS_IMAGE: |
| 420 |
PUTS(" (IMG) "); |
| 421 |
break; |
| 422 |
case GOPHER_SOUND: |
| 423 |
case GOPHER_PLUS_SOUND: |
| 424 |
PUTS(" (SND) "); |
| 425 |
break; |
| 426 |
case GOPHER_MACBINHEX: |
| 427 |
PUTS(" (HQX) "); |
| 428 |
break; |
| 429 |
case GOPHER_HTML: |
| 430 |
case GOPHER_CHTML: |
| 431 |
PUTS("(HTML) "); |
| 432 |
break; |
| 433 |
case 'm': |
| 434 |
PUTS("(MIME) "); |
| 435 |
break; |
| 436 |
case GOPHER_PLUS_MOVIE: |
| 437 |
PUTS(" (MOV) "); |
| 438 |
break; |
| 439 |
case GOPHER_PLUS_PDF: |
| 440 |
PUTS(" (PDF) "); |
| 441 |
break; |
| 442 |
default: |
| 443 |
PUTS("(UNKN) "); |
| 444 |
break; |
| 445 |
} |
| 446 |
|
| 447 |
if (gtype != GOPHER_DUPLICATE) |
| 448 |
this_type = gtype; |
| 449 |
|
| 450 |
HTSprintf0(&address, "//%s/%c", host, this_type); |
| 451 |
if (gtype == GOPHER_HTML) { |
| 452 |
valid_chars = acceptable_html; |
| 453 |
if (*selector == '/') |
| 454 |
++selector; |
| 455 |
} else { |
| 456 |
valid_chars = acceptable_file; |
| 457 |
} |
| 458 |
|
| 459 |
for (r = selector; *r; r++) { /* Encode selector string */ |
| 460 |
if (valid_chars[UCH(*r)]) { |
| 461 |
HTSprintf(&address, "%c", *r); |
| 462 |
} else { |
| 463 |
HTSprintf(&address, "%c%c%c", |
| 464 |
HEX_ESCAPE, /* Means hex coming */ |
| 465 |
hex[(TOASCII(*r)) >> 4], |
| 466 |
hex[(TOASCII(*r)) & 15]); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
/* Error response from Gopher doesn't deserve to |
| 471 |
be a hyperlink. */ |
| 472 |
if (strcmp(address, "gopher://error.host:1/0")) |
| 473 |
write_anchor(name, address); |
| 474 |
else |
| 475 |
PUTS(name); |
| 476 |
FREE(address); |
| 477 |
} else { /* parse error */ |
| 478 |
CTRACE((tfp, "HTGopher: Bad menu item (type %d, port %s).\n", |
| 479 |
gtype, NonNull(port))); |
| 480 |
PUTS(line); |
| 481 |
|
| 482 |
} /* parse error */ |
| 483 |
|
| 484 |
PUTC('\n'); |
| 485 |
p = line; /* Start again at beginning of line */ |
| 486 |
|
| 487 |
} /* if end of line */ |
| 488 |
|
| 489 |
} /* Loop over characters */ |
| 490 |
|
| 491 |
end_html: |
| 492 |
END(HTML_PRE); |
| 493 |
PUTC('\n'); |
| 494 |
END(HTML_BODY); |
| 495 |
PUTC('\n'); |
| 496 |
END(HTML_HTML); |
| 497 |
PUTC('\n'); |
| 498 |
FREE_TARGET; |
| 499 |
|
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
/* Parse a Gopher CSO document from an ISINDEX query. |
| 504 |
* ================================================== |
| 505 |
* |
| 506 |
* Accepts an open socket to a CSO server waiting to send us |
| 507 |
* data and puts it on the screen in a reasonable manner. |
| 508 |
* |
| 509 |
* Perhaps this data can be automatically linked to some |
| 510 |
* other source as well??? |
| 511 |
* |
| 512 |
* Taken from hacking by Lou [email protected] |
| 513 |
* on XMosaic-1.1, and put on libwww 2.11 by Arthur Secret, |
| 514 |
* [email protected] . |
| 515 |
*/ |
| 516 |
static void parse_cso(const char *arg, |
| 517 |
HTParentAnchor *anAnchor) |
| 518 |
{ |
| 519 |
int ich; |
| 520 |
char line[BIG]; |
| 521 |
char *p = line; |
| 522 |
char *first_colon, *second_colon, last_char = '\0'; |
| 523 |
const char *title; |
| 524 |
|
| 525 |
START(HTML_HEAD); |
| 526 |
PUTC('\n'); |
| 527 |
START(HTML_TITLE); |
| 528 |
if ((title = HTAnchor_title(anAnchor))) |
| 529 |
PUTS(title); |
| 530 |
else |
| 531 |
PUTS(GOPHER_CSO_SEARCH_RESULTS); |
| 532 |
END(HTML_TITLE); |
| 533 |
PUTC('\n'); |
| 534 |
END(HTML_HEAD); |
| 535 |
PUTC('\n'); |
| 536 |
START(HTML_H1); |
| 537 |
if ((title = HTAnchor_title(anAnchor))) |
| 538 |
PUTS(title); |
| 539 |
else { |
| 540 |
PUTS(arg); |
| 541 |
PUTS(GOPHER_SEARCH_RESULTS); |
| 542 |
} |
| 543 |
END(HTML_H1); |
| 544 |
PUTC('\n'); |
| 545 |
START(HTML_PRE); |
| 546 |
|
| 547 |
/* |
| 548 |
* Start grabbing chars from the network. |
| 549 |
*/ |
| 550 |
while ((ich = NEXT_CHAR) != EOF) { |
| 551 |
if ((char) ich != LF) { |
| 552 |
*p = (char) ich; /* Put character in line */ |
| 553 |
if (p < &line[BIG - 1]) |
| 554 |
p++; |
| 555 |
} else { |
| 556 |
*p = '\0'; /* Terminate line */ |
| 557 |
p = line; /* Scan it to parse it */ |
| 558 |
/* |
| 559 |
* OK we now have a line in 'p'. Lets parse it and print it. |
| 560 |
*/ |
| 561 |
|
| 562 |
/* |
| 563 |
* Break on line that begins with a 2. It's the end of data. |
| 564 |
*/ |
| 565 |
if (*p == '2') |
| 566 |
break; |
| 567 |
|
| 568 |
/* |
| 569 |
* Lines beginning with 5 are errors. Print them and quit. |
| 570 |
*/ |
| 571 |
if (*p == '5') { |
| 572 |
START(HTML_H2); |
| 573 |
PUTS(p + 4); |
| 574 |
END(HTML_H2); |
| 575 |
break; |
| 576 |
} |
| 577 |
|
| 578 |
if (*p == '-') { |
| 579 |
/* |
| 580 |
* Data lines look like -200:#: |
| 581 |
* where # is the search result number and can be multiple |
| 582 |
* digits (infinite?). |
| 583 |
* Find the second colon and check the digit to the left of it |
| 584 |
* to see if they are different. If they are then a different |
| 585 |
* person is starting. Make this line an <h2>. |
| 586 |
*/ |
| 587 |
|
| 588 |
/* |
| 589 |
* Find the second_colon. |
| 590 |
*/ |
| 591 |
second_colon = NULL; |
| 592 |
first_colon = StrChr(p, ':'); |
| 593 |
if (first_colon != NULL) { |
| 594 |
second_colon = StrChr(first_colon + 1, ':'); |
| 595 |
} |
| 596 |
|
| 597 |
if (second_colon != NULL) { /* error check */ |
| 598 |
|
| 599 |
if (*(second_colon - 1) != last_char) |
| 600 |
/* print separator */ |
| 601 |
{ |
| 602 |
END(HTML_PRE); |
| 603 |
START(HTML_H2); |
| 604 |
} |
| 605 |
|
| 606 |
/* |
| 607 |
* Right now the record appears with the alias (first line) |
| 608 |
* as the header and the rest as <pre> text. |
| 609 |
* |
| 610 |
* It might look better with the name as the header and the |
| 611 |
* rest as a <ul> with <li> tags. I'm not sure whether the |
| 612 |
* name field comes in any special order or if its even |
| 613 |
* required in a record, so for now the first line is the |
| 614 |
* header no matter what it is (it's almost always the |
| 615 |
* alias). |
| 616 |
* |
| 617 |
* A <dl> with the first line as the <DT> and the rest as |
| 618 |
* some form of <DD> might good also? |
| 619 |
*/ |
| 620 |
|
| 621 |
/* |
| 622 |
* Print data. |
| 623 |
*/ |
| 624 |
PUTS(second_colon + 1); |
| 625 |
PUTC('\n'); |
| 626 |
|
| 627 |
if (*(second_colon - 1) != last_char) |
| 628 |
/* end separator */ |
| 629 |
{ |
| 630 |
END(HTML_H2); |
| 631 |
START(HTML_PRE); |
| 632 |
} |
| 633 |
|
| 634 |
/* |
| 635 |
* Save the char before the second colon for comparison on |
| 636 |
* the next pass. |
| 637 |
*/ |
| 638 |
last_char = *(second_colon - 1); |
| 639 |
|
| 640 |
} /* end if second_colon */ |
| 641 |
} /* end if *p == '-' */ |
| 642 |
} /* if end of line */ |
| 643 |
|
| 644 |
} /* Loop over characters */ |
| 645 |
|
| 646 |
/* end the text block */ |
| 647 |
PUTC('\n'); |
| 648 |
END(HTML_PRE); |
| 649 |
PUTC('\n'); |
| 650 |
FREE_TARGET; |
| 651 |
|
| 652 |
return; /* all done */ |
| 653 |
} /* end of procedure */ |
| 654 |
|
| 655 |
/* Display a Gopher CSO ISINDEX cover page. |
| 656 |
* ======================================== |
| 657 |
*/ |
| 658 |
static void display_cso(const char *arg, |
| 659 |
HTParentAnchor *anAnchor) |
| 660 |
{ |
| 661 |
const char *title; |
| 662 |
|
| 663 |
START(HTML_HEAD); |
| 664 |
PUTC('\n'); |
| 665 |
START(HTML_TITLE); |
| 666 |
if ((title = HTAnchor_title(anAnchor))) |
| 667 |
PUTS(title); |
| 668 |
else |
| 669 |
PUTS(GOPHER_CSO_INDEX); |
| 670 |
END(HTML_TITLE); |
| 671 |
PUTC('\n'); |
| 672 |
START(HTML_ISINDEX); |
| 673 |
PUTC('\n'); |
| 674 |
END(HTML_HEAD); |
| 675 |
PUTC('\n'); |
| 676 |
START(HTML_H1); |
| 677 |
if ((title = HTAnchor_title(anAnchor))) |
| 678 |
PUTS(title); |
| 679 |
else { |
| 680 |
PUTS(arg); |
| 681 |
PUTS(INDEX_SEGMENT); |
| 682 |
} |
| 683 |
END(HTML_H1); |
| 684 |
PUTS(GOPHER_CSO_INDEX_SUBTITLE); |
| 685 |
START(HTML_P); |
| 686 |
PUTS(GOPHER_CSO_SOLICIT_KEYWORDS); |
| 687 |
START(HTML_P); |
| 688 |
PUTS(SEGMENT_KEYWORDS_WILL); |
| 689 |
PUTS(SEGMENT_PERSONS_DB_NAME); |
| 690 |
|
| 691 |
if (!HTAnchor_title(anAnchor)) |
| 692 |
HTAnchor_setTitle(anAnchor, arg); |
| 693 |
|
| 694 |
FREE_TARGET; |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
/* Display a Gopher Index document. |
| 699 |
* ================================ |
| 700 |
*/ |
| 701 |
static void display_index(const char *arg, |
| 702 |
HTParentAnchor *anAnchor) |
| 703 |
{ |
| 704 |
const char *title; |
| 705 |
|
| 706 |
START(HTML_HEAD); |
| 707 |
PUTC('\n'); |
| 708 |
PUTC('\n'); |
| 709 |
START(HTML_TITLE); |
| 710 |
if ((title = HTAnchor_title(anAnchor))) |
| 711 |
PUTS(title); |
| 712 |
else |
| 713 |
PUTS(GOPHER_INDEX_TITLE); |
| 714 |
END(HTML_TITLE); |
| 715 |
PUTC('\n'); |
| 716 |
START(HTML_ISINDEX); |
| 717 |
PUTC('\n'); |
| 718 |
END(HTML_HEAD); |
| 719 |
PUTC('\n'); |
| 720 |
START(HTML_H1); |
| 721 |
if ((title = HTAnchor_title(anAnchor))) |
| 722 |
PUTS(title); |
| 723 |
else { |
| 724 |
PUTS(arg); |
| 725 |
PUTS(INDEX_SEGMENT); |
| 726 |
} |
| 727 |
END(HTML_H1); |
| 728 |
PUTS(GOPHER_INDEX_SUBTITLE); |
| 729 |
START(HTML_P); |
| 730 |
PUTS(GOPHER_SOLICIT_KEYWORDS); |
| 731 |
|
| 732 |
if (!HTAnchor_title(anAnchor)) |
| 733 |
HTAnchor_setTitle(anAnchor, arg); |
| 734 |
|
| 735 |
FREE_TARGET; |
| 736 |
return; |
| 737 |
} |
| 738 |
|
| 739 |
/* De-escape a selector into a command. |
| 740 |
* ==================================== |
| 741 |
* |
| 742 |
* The % hex escapes are converted. Otherwise, the string is copied. |
| 743 |
*/ |
| 744 |
static void de_escape(char *command, const char *selector) |
| 745 |
{ |
| 746 |
const char *p = selector; |
| 747 |
char *q; |
| 748 |
|
| 749 |
if (command == NULL) |
| 750 |
outofmem(__FILE__, "HTLoadGopher"); |
| 751 |
|
| 752 |
q = command; |
| 753 |
while (*p) { /* Decode hex */ |
| 754 |
if (*p == HEX_ESCAPE) { |
| 755 |
char c; |
| 756 |
unsigned int b; |
| 757 |
|
| 758 |
p++; |
| 759 |
c = *p++; |
| 760 |
b = UCH(from_hex(c)); |
| 761 |
c = *p++; |
| 762 |
if (!c) |
| 763 |
break; /* Odd number of chars! */ |
| 764 |
*q++ = (char) FROMASCII((b << 4) + UCH(from_hex(c))); |
| 765 |
} else { |
| 766 |
*q++ = *p++; /* Record */ |
| 767 |
} |
| 768 |
} |
| 769 |
*q = '\0'; /* Terminate command */ |
| 770 |
} |
| 771 |
|
| 772 |
/* Free the CSOfields structures. - FM |
| 773 |
* =================================== |
| 774 |
*/ |
| 775 |
static void free_CSOfields(void) |
| 776 |
{ |
| 777 |
CSOfield_info *cur = CSOfields; |
| 778 |
CSOfield_info *prev; |
| 779 |
|
| 780 |
while (cur) { |
| 781 |
if (cur->name != cur->name_buf) |
| 782 |
FREE(cur->name); |
| 783 |
if (cur->attributes != cur->attr_buf) |
| 784 |
FREE(cur->attributes); |
| 785 |
if (cur->description != cur->desc_buf) |
| 786 |
FREE(cur->description); |
| 787 |
prev = cur; |
| 788 |
cur = cur->next; |
| 789 |
FREE(prev); |
| 790 |
} |
| 791 |
|
| 792 |
return; |
| 793 |
} |
| 794 |
|
| 795 |
/* Interpret CSO/PH form template keys. - FM |
| 796 |
* ========================================= |
| 797 |
*/ |
| 798 |
static void interpret_cso_key(const char *key, |
| 799 |
char *buf, |
| 800 |
size_t bufsize, |
| 801 |
int *length, |
| 802 |
CSOformgen_context * ctx, |
| 803 |
HTStream *Target) |
| 804 |
{ |
| 805 |
CSOfield_info *fld; |
| 806 |
|
| 807 |
if ((fld = ctx->fld) != NULL) { |
| 808 |
/* |
| 809 |
* Most substitutions only recognized inside of loops. |
| 810 |
*/ |
| 811 |
int error = 0; |
| 812 |
|
| 813 |
if (0 == StrNCmp(key, "$(FID)", 6)) { |
| 814 |
sprintf(buf, "%d", fld->id); |
| 815 |
} else if (0 == StrNCmp(key, "$(FDESC)", 8)) { |
| 816 |
sprintf(buf, "%.2046s", fld->description); |
| 817 |
} else if (0 == StrNCmp(key, "$(FDEF)", 7)) { |
| 818 |
strcpy(buf, fld->defreturn ? " checked" : ""); |
| 819 |
} else if (0 == StrNCmp(key, "$(FNDX)", 7)) { |
| 820 |
strcpy(buf, fld->indexed ? "*" : ""); |
| 821 |
} else if (0 == StrNCmp(key, "$(FSIZE)", 8)) { |
| 822 |
sprintf(buf, " size=%d maxlength=%d", |
| 823 |
fld->max_size > 55 ? 55 : fld->max_size, |
| 824 |
fld->max_size); |
| 825 |
} else if (0 == StrNCmp(key, "$(FSIZE2)", 9)) { |
| 826 |
sprintf(buf, " maxlength=%d", fld->max_size); |
| 827 |
} else { |
| 828 |
error = 1; |
| 829 |
} |
| 830 |
if (!error) { |
| 831 |
*length = (int) strlen(buf); |
| 832 |
return; |
| 833 |
} |
| 834 |
} |
| 835 |
buf[0] = '\0'; |
| 836 |
if (0 == StrNCmp(key, "$(NEXTFLD)", 10)) { |
| 837 |
if (!ctx->fld) |
| 838 |
fld = CSOfields; |
| 839 |
else |
| 840 |
fld = ctx->fld->next; |
| 841 |
switch (ctx->field_select) { |
| 842 |
case 0: |
| 843 |
/* |
| 844 |
* 'Query' fields, public and lookup attributes. |
| 845 |
*/ |
| 846 |
for (; fld; fld = fld->next) |
| 847 |
if (fld->gpublic && (fld->lookup == 1)) |
| 848 |
break; |
| 849 |
break; |
| 850 |
case 1: |
| 851 |
/* |
| 852 |
* 'Query' fields, accept lookup attribute. |
| 853 |
*/ |
| 854 |
for (; fld; fld = fld->next) |
| 855 |
if (fld->lookup == 1) |
| 856 |
break; |
| 857 |
break; |
| 858 |
case 2: |
| 859 |
/* |
| 860 |
* 'Return' fields, public only. |
| 861 |
*/ |
| 862 |
for (; fld; fld = fld->next) |
| 863 |
if (fld->gpublic) |
| 864 |
break; |
| 865 |
break; |
| 866 |
case 3: |
| 867 |
/* |
| 868 |
* All fields. |
| 869 |
*/ |
| 870 |
break; |
| 871 |
} |
| 872 |
if (fld) { |
| 873 |
ctx->cur_line = ctx->rep_line; |
| 874 |
ctx->cur_off = ctx->rep_off; |
| 875 |
} |
| 876 |
ctx->fld = fld; |
| 877 |
|
| 878 |
} else if ((0 == StrNCmp(key, "$(QFIELDS)", 10)) || |
| 879 |
(0 == StrNCmp(key, "$(RFIELDS)", 10))) { |
| 880 |
/* |
| 881 |
* Begin iteration sequence. |
| 882 |
*/ |
| 883 |
ctx->rep_line = ctx->cur_line; |
| 884 |
ctx->rep_off = ctx->cur_off; |
| 885 |
ctx->fld = (CSOfield_info *) 0; |
| 886 |
ctx->seek = "$(NEXTFLD)"; |
| 887 |
ctx->field_select = (key[2] == 'Q') ? 0 : 2; |
| 888 |
if (ctx->public_override) |
| 889 |
ctx->field_select++; |
| 890 |
|
| 891 |
} else if (0 == StrNCmp(key, "$(NAMEFLD)", 10)) { |
| 892 |
/* |
| 893 |
* Special, locate name field. Flag lookup so QFIELDS will skip it. |
| 894 |
*/ |
| 895 |
for (fld = CSOfields; fld; fld = fld->next) |
| 896 |
if (strcmp(fld->name, "name") == 0 || |
| 897 |
strcmp(fld->name, "Name") == 0) { |
| 898 |
if (fld->lookup) |
| 899 |
fld->lookup = 2; |
| 900 |
break; |
| 901 |
} |
| 902 |
ctx->fld = fld; |
| 903 |
} else if (0 == StrNCmp(key, "$(HOST)", 7)) { |
| 904 |
strcpy(buf, ctx->host); |
| 905 |
} else if (0 == StrNCmp(key, "$(PORT)", 7)) { |
| 906 |
sprintf(buf, "%d", ctx->port); |
| 907 |
} else { |
| 908 |
/* |
| 909 |
* No match, dump key to buffer so client sees it for debugging. |
| 910 |
*/ |
| 911 |
size_t out = 0; |
| 912 |
|
| 913 |
while (*key && (*key != ')')) { |
| 914 |
buf[out++] = (*key++); |
| 915 |
if (out > bufsize - 2) { |
| 916 |
buf[out] = '\0'; |
| 917 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 918 |
out = 0; |
| 919 |
} |
| 920 |
} |
| 921 |
buf[out++] = ')'; |
| 922 |
buf[out] = '\0'; |
| 923 |
} |
| 924 |
*length = (int) strlen(buf); |
| 925 |
return; |
| 926 |
} |
| 927 |
|
| 928 |
/* Parse the elements in a CSO/PH fields structure. - FM |
| 929 |
* ===================================================== |
| 930 |
*/ |
| 931 |
static int parse_cso_field_info(CSOfield_info *blk) |
| 932 |
{ |
| 933 |
char *info, *max_spec; |
| 934 |
|
| 935 |
/* |
| 936 |
* Initialize all fields to default values. |
| 937 |
*/ |
| 938 |
blk->indexed = blk->lookup = blk->reserved = blk->max_size = blk->url = 0; |
| 939 |
blk->defreturn = blk->explicit_return = blk->gpublic = 0; |
| 940 |
|
| 941 |
/* |
| 942 |
* Search for keywords in info string and set values. Attributes are |
| 943 |
* converted to all lower-case for comparison. |
| 944 |
*/ |
| 945 |
info = blk->attributes; |
| 946 |
LYLowerCase(info); |
| 947 |
if (strstr(info, "indexed ")) |
| 948 |
blk->indexed = 1; |
| 949 |
if (strstr(info, "default ")) |
| 950 |
blk->defreturn = 1; |
| 951 |
if (strstr(info, "public ")) |
| 952 |
blk->gpublic = 1; |
| 953 |
if (strstr(info, "lookup ")) |
| 954 |
blk->lookup = 1; |
| 955 |
if (strstr(info, "url ")) { |
| 956 |
blk->url = 1; |
| 957 |
blk->defreturn = 1; |
| 958 |
} |
| 959 |
max_spec = strstr(info, "max "); |
| 960 |
if (max_spec) { |
| 961 |
sscanf(&max_spec[4], "%d", &blk->max_size); |
| 962 |
} else { |
| 963 |
blk->max_size = 32; |
| 964 |
} |
| 965 |
|
| 966 |
return 0; |
| 967 |
} |
| 968 |
|
| 969 |
/* Parse a reply from a CSO/PH fields request. - FM |
| 970 |
* ================================================ |
| 971 |
*/ |
| 972 |
static int parse_cso_fields(char *buf, |
| 973 |
size_t size) |
| 974 |
{ |
| 975 |
int ich; |
| 976 |
char *p = buf; |
| 977 |
int i, code = 0, prev_code; |
| 978 |
size_t alen; |
| 979 |
char *indx, *name; |
| 980 |
CSOfield_info *last, *newf; |
| 981 |
|
| 982 |
last = CSOfields = (CSOfield_info *) 0; |
| 983 |
prev_code = -2555; |
| 984 |
buf[0] = '\0'; |
| 985 |
|
| 986 |
/* |
| 987 |
* Start grabbing chars from the network. |
| 988 |
*/ |
| 989 |
while ((ich = NEXT_CHAR) != EOF) { |
| 990 |
if (interrupted_in_htgetcharacter) { |
| 991 |
CTRACE((tfp, |
| 992 |
"HTLoadCSO: Interrupted in HTGetCharacter, apparently.\n")); |
| 993 |
free_CSOfields(); |
| 994 |
buf[0] = '\0'; |
| 995 |
return HT_INTERRUPTED; |
| 996 |
} |
| 997 |
|
| 998 |
if ((char) ich != LF) { |
| 999 |
*p = (char) ich; /* Put character in buffer */ |
| 1000 |
if (p < &buf[size - 1]) { |
| 1001 |
p++; |
| 1002 |
} |
| 1003 |
} else { |
| 1004 |
*p = '\0'; /* Terminate line */ |
| 1005 |
p = buf; /* Scan it to parse it */ |
| 1006 |
|
| 1007 |
/* OK we now have a line in 'p' lets parse it. |
| 1008 |
*/ |
| 1009 |
|
| 1010 |
/* |
| 1011 |
* Break on line that begins with a 2. It's the end of data. |
| 1012 |
*/ |
| 1013 |
if (*p == '2') |
| 1014 |
break; |
| 1015 |
|
| 1016 |
/* |
| 1017 |
* Lines beginning with 5 are errors. Print them and quit. |
| 1018 |
*/ |
| 1019 |
if (*p == '5') { |
| 1020 |
return 5; |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (*p == '-') { |
| 1024 |
/* |
| 1025 |
* Data lines look like -200:#: |
| 1026 |
* where # is the search result number and can be multiple |
| 1027 |
* digits (infinite?). |
| 1028 |
*/ |
| 1029 |
|
| 1030 |
/* |
| 1031 |
* Check status, ignore any non-success. |
| 1032 |
*/ |
| 1033 |
if (p[1] != '2') |
| 1034 |
continue; |
| 1035 |
|
| 1036 |
/* |
| 1037 |
* Parse fields within returned line into status, ndx, name, |
| 1038 |
* data. |
| 1039 |
*/ |
| 1040 |
indx = NULL; |
| 1041 |
name = NULL; |
| 1042 |
for (i = 0; p[i]; i++) { |
| 1043 |
if (p[i] == ':') { |
| 1044 |
p[i] = '\0'; |
| 1045 |
if (!indx) { |
| 1046 |
indx = (char *) &p[i + 1]; |
| 1047 |
code = atoi(indx); |
| 1048 |
} else if (!name) { |
| 1049 |
name = (char *) &p[i + 1]; |
| 1050 |
} else { |
| 1051 |
i++; |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
} |
| 1056 |
/* |
| 1057 |
* Add data to field structure. |
| 1058 |
*/ |
| 1059 |
if (name) { |
| 1060 |
if (code == prev_code) { |
| 1061 |
/* |
| 1062 |
* Remaining data are description. Save in current |
| 1063 |
* info block. |
| 1064 |
*/ |
| 1065 |
if (last != NULL) { |
| 1066 |
alen = strlen((char *) &p[i]) + 1; |
| 1067 |
if (alen > sizeof(last->desc_buf)) { |
| 1068 |
if (last->description != last->desc_buf) |
| 1069 |
FREE(last->description); |
| 1070 |
if (!(last->description = (char *) malloc(alen))) { |
| 1071 |
outofmem(__FILE__, "HTLoadCSO"); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
strcpy(last->description, (char *) &p[i]); |
| 1075 |
} |
| 1076 |
} else { |
| 1077 |
/* |
| 1078 |
* Initialize new block, append to end of list to |
| 1079 |
* preserve order. |
| 1080 |
*/ |
| 1081 |
newf = typecalloc(CSOfield_info); |
| 1082 |
|
| 1083 |
if (!newf) { |
| 1084 |
outofmem(__FILE__, "HTLoadCSO"); |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (last) |
| 1088 |
last->next = newf; |
| 1089 |
else |
| 1090 |
CSOfields = newf; |
| 1091 |
last = newf; |
| 1092 |
|
| 1093 |
newf->next = (CSOfield_info *) 0; |
| 1094 |
newf->name = newf->name_buf; |
| 1095 |
alen = strlen(name) + 1; |
| 1096 |
if (alen > sizeof(newf->name_buf)) { |
| 1097 |
if (!(newf->name = (char *) malloc(alen))) { |
| 1098 |
outofmem(__FILE__, "HTLoadCSO"); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
strcpy(newf->name, name); |
| 1102 |
|
| 1103 |
newf->attributes = newf->attr_buf; |
| 1104 |
alen = strlen((char *) &p[i]) + 2; |
| 1105 |
if (alen > sizeof(newf->attr_buf)) { |
| 1106 |
if (!(newf->attributes = (char *) malloc(alen))) { |
| 1107 |
outofmem(__FILE__, "HTLoadCSO"); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
strcpy(newf->attributes, (char *) &p[i]); |
| 1111 |
strcpy((char *) &newf->attributes[alen - 2], " "); |
| 1112 |
newf->description = newf->desc_buf; |
| 1113 |
newf->desc_buf[0] = '\0'; |
| 1114 |
newf->id = atoi(indx); |
| 1115 |
/* |
| 1116 |
* Scan for keywords. |
| 1117 |
*/ |
| 1118 |
parse_cso_field_info(newf); |
| 1119 |
} |
| 1120 |
prev_code = code; |
| 1121 |
} else |
| 1122 |
break; |
| 1123 |
} /* end if *p == '-' */ |
| 1124 |
} /* if end of line */ |
| 1125 |
|
| 1126 |
} /* Loop over characters */ |
| 1127 |
|
| 1128 |
/* end the text block */ |
| 1129 |
|
| 1130 |
if (buf[0] == '\0') { |
| 1131 |
return -1; /* no response */ |
| 1132 |
} |
| 1133 |
buf[0] = '\0'; |
| 1134 |
return 0; /* all done */ |
| 1135 |
} /* end of procedure */ |
| 1136 |
|
| 1137 |
/* Generate a form for submitting CSO/PH searches. - FM |
| 1138 |
* ==================================================== |
| 1139 |
*/ |
| 1140 |
static int generate_cso_form(char *host, |
| 1141 |
int port, |
| 1142 |
char *buf, |
| 1143 |
size_t bufsize, |
| 1144 |
HTStream *Target) |
| 1145 |
{ |
| 1146 |
int i, j, length; |
| 1147 |
size_t out; |
| 1148 |
int full_flag = 1; |
| 1149 |
const char *key; |
| 1150 |
const char *line; |
| 1151 |
CSOformgen_context ctx; |
| 1152 |
static const char *ctemplate[] = |
| 1153 |
{ |
| 1154 |
"<HTML>\n<HEAD>\n<TITLE>CSO/PH Query Form for $(HOST)</TITLE>\n</HEAD>\n<BODY>", |
| 1155 |
"<H2><I>CSO/PH Query Form</I> for <EM>$(HOST)</EM></H2>", |
| 1156 |
"To search the database for a name, fill in one or more of the fields", |
| 1157 |
"in the form below and activate the 'Submit query' button. At least", |
| 1158 |
"one of the entered fields must be flagged as indexed.", |
| 1159 |
"<HR><FORM method=\"POST\" action=\"cso://$(HOST)/\">", |
| 1160 |
"[ <input type=\"submit\" value=\"Submit query\"> | ", |
| 1161 |
"<input type=\"reset\" value=\"Clear fields\"> ]", |
| 1162 |
"<P><DL>", |
| 1163 |
" <DT>Search parameters (* indicates indexed field):", |
| 1164 |
" <DD>", |
| 1165 |
"$(NAMEFLD) <DL COMPACT>\n <DT><I>$(FDESC)</I>$(FNDX)", |
| 1166 |
" <DD>Last: <input name=\"q_$(FID)\" type=\"text\" size=49$(FSIZE2)>", |
| 1167 |
" <DD>First: <input name=\"q_$(FID)\" type=\"text\" size=48$(FSIZE2)>", |
| 1168 |
"$(QFIELDS) <DT><I>$(FDESC)</I>$(FNDX)", |
| 1169 |
" <DD><input name=\"q_$(FID)\" type=\"text\" $(FSIZE)>\n$(NEXTFLD)", |
| 1170 |
" </DL>", |
| 1171 |
" </DL>\n<P><DL>", |
| 1172 |
" <DT>Output format:", |
| 1173 |
" <DD>Returned data option: <select name=\"return\">", |
| 1174 |
" <option>default<option selected>all<option>selected</select><BR>", |
| 1175 |
"$(RFIELDS) <input type=\"checkbox\" name=\"r_$(FID)\"$(FDEF)> $(FDESC)<BR>", |
| 1176 |
"$(NEXTFLD) ", |
| 1177 |
" </DL></FORM><HR>\n</BODY>\n</HTML>", |
| 1178 |
(char *) 0 |
| 1179 |
}; |
| 1180 |
|
| 1181 |
memset(&ctx, 0, sizeof(ctx)); |
| 1182 |
ctx.host = host; |
| 1183 |
ctx.seek = (char *) 0; |
| 1184 |
ctx.port = port; |
| 1185 |
ctx.fld = (CSOfield_info *) 0; |
| 1186 |
ctx.public_override = full_flag; |
| 1187 |
/* |
| 1188 |
* Parse the strings in the template array to produce HTML document to send |
| 1189 |
* to client. First line is skipped for 'full' lists. |
| 1190 |
*/ |
| 1191 |
out = 0; |
| 1192 |
buf[out] = '\0'; |
| 1193 |
for (i = 0; ctemplate[i]; i++) { |
| 1194 |
/* |
| 1195 |
* Search the current string for substitution, flagged by $( |
| 1196 |
*/ |
| 1197 |
for (line = ctemplate[i], j = 0; line[j]; j++) { |
| 1198 |
if ((line[j] == '$') && (line[j + 1] == '(')) { |
| 1199 |
/* |
| 1200 |
* Command detected, flush output buffer and find closing ')' |
| 1201 |
* that delimits the command. |
| 1202 |
*/ |
| 1203 |
buf[out] = '\0'; |
| 1204 |
if (out > 0) |
| 1205 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1206 |
for (key = &line[j]; line[j + 1] && (line[j] != ')'); j++) { |
| 1207 |
; |
| 1208 |
} |
| 1209 |
/* |
| 1210 |
* Save context, interpret command and restore updated context. |
| 1211 |
*/ |
| 1212 |
ctx.cur_line = i; |
| 1213 |
ctx.cur_off = j; |
| 1214 |
interpret_cso_key(key, buf, bufsize, &length, &ctx, Target); |
| 1215 |
i = ctx.cur_line; |
| 1216 |
j = ctx.cur_off; |
| 1217 |
line = ctemplate[i]; |
| 1218 |
out = (size_t) length; |
| 1219 |
|
| 1220 |
if (ctx.seek) { |
| 1221 |
/* |
| 1222 |
* Command wants us to skip (forward) to indicated token. |
| 1223 |
* Start at current position. |
| 1224 |
*/ |
| 1225 |
size_t slen = strlen(ctx.seek); |
| 1226 |
|
| 1227 |
for (; ctemplate[i]; i++) { |
| 1228 |
for (line = ctemplate[i]; line[j]; j++) { |
| 1229 |
if (line[j] == '$') |
| 1230 |
if (0 == StrNCmp(ctx.seek, &line[j], slen)) { |
| 1231 |
if (j == 0) |
| 1232 |
j = (int) strlen(ctemplate[--i]) - 1; |
| 1233 |
else |
| 1234 |
--j; |
| 1235 |
line = ctemplate[i]; |
| 1236 |
ctx.seek = (char *) 0; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
if (!ctx.seek) |
| 1241 |
break; |
| 1242 |
j = 0; |
| 1243 |
} |
| 1244 |
if (ctx.seek) { |
| 1245 |
char *temp = NULL; |
| 1246 |
|
| 1247 |
HTSprintf0(&temp, GOPHER_CSO_SEEK_FAILED, ctx.seek); |
| 1248 |
(*Target->isa->put_block) (Target, temp, (int) strlen(temp)); |
| 1249 |
FREE(temp); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
} else { |
| 1253 |
/* |
| 1254 |
* Non-command text, add to output buffer. |
| 1255 |
*/ |
| 1256 |
buf[out++] = line[j]; |
| 1257 |
if (out > (bufsize - 3)) { |
| 1258 |
buf[out] = '\0'; |
| 1259 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1260 |
out = 0; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
} |
| 1264 |
buf[out++] = '\n'; |
| 1265 |
buf[out] = '\0'; |
| 1266 |
} |
| 1267 |
if (out > 0) |
| 1268 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1269 |
|
| 1270 |
return 0; |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* Generate a results report for CSO/PH form-based searches. - FM |
| 1274 |
* ============================================================== |
| 1275 |
*/ |
| 1276 |
static int generate_cso_report(HTStream *Target) |
| 1277 |
{ |
| 1278 |
int ich; |
| 1279 |
char line[BIG]; |
| 1280 |
char *buf = NULL; |
| 1281 |
char *p = line, *href = NULL; |
| 1282 |
int i, prev_ndx, ndx; |
| 1283 |
char *rcode, *ndx_str, *fname, *fvalue, *l; |
| 1284 |
CSOfield_info *fld; |
| 1285 |
BOOL stop = FALSE; |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* Read lines until non-negative status. |
| 1289 |
*/ |
| 1290 |
prev_ndx = -100; |
| 1291 |
/* |
| 1292 |
* Start grabbing chars from the network. |
| 1293 |
*/ |
| 1294 |
while (!stop && (ich = NEXT_CHAR) != EOF) { |
| 1295 |
if (interrupted_in_htgetcharacter) { |
| 1296 |
CTRACE((tfp, |
| 1297 |
"HTLoadCSO: Interrupted in HTGetCharacter, apparently.\n")); |
| 1298 |
_HTProgress(CONNECTION_INTERRUPTED); |
| 1299 |
goto end_CSOreport; |
| 1300 |
} |
| 1301 |
|
| 1302 |
if ((char) ich != LF) { |
| 1303 |
*p = (char) ich; /* Put character in line */ |
| 1304 |
if (p < &line[BIG - 1]) { |
| 1305 |
p++; |
| 1306 |
} |
| 1307 |
} else { |
| 1308 |
*p = '\0'; /* Terminate line */ |
| 1309 |
/* |
| 1310 |
* OK we now have a line. Load it as 'p' and parse it. |
| 1311 |
*/ |
| 1312 |
p = line; |
| 1313 |
if (p[0] != '-' && p[0] != '1') { |
| 1314 |
stop = TRUE; |
| 1315 |
} |
| 1316 |
rcode = (p[0] == '-') ? &p[1] : p; |
| 1317 |
ndx_str = fname = NULL; |
| 1318 |
for (i = 0; p[i] != '\0'; i++) { |
| 1319 |
if (p[i] == ':') { |
| 1320 |
p[i] = '\0'; |
| 1321 |
fname = &p[i + 1]; |
| 1322 |
if (ndx_str) { |
| 1323 |
break; |
| 1324 |
} |
| 1325 |
ndx_str = fname; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
if (ndx_str) { |
| 1329 |
ndx = atoi(ndx_str); |
| 1330 |
if (prev_ndx != ndx) { |
| 1331 |
if (prev_ndx != -100) { |
| 1332 |
HTSprintf0(&buf, "</DL></DL>\n"); |
| 1333 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1334 |
} |
| 1335 |
if (ndx == 0) { |
| 1336 |
HTSprintf0(&buf, |
| 1337 |
"<HR><DL><DT>Information/status<DD><DL><DT>\n"); |
| 1338 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1339 |
} else { |
| 1340 |
HTSprintf0(&buf, |
| 1341 |
"<HR><DL><DT>Entry %d:<DD><DL COMPACT><DT>\n", ndx); |
| 1342 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1343 |
} |
| 1344 |
prev_ndx = ndx; |
| 1345 |
} |
| 1346 |
} else { |
| 1347 |
HTSprintf0(&buf, "<DD>%s\n", rcode); |
| 1348 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1349 |
continue; |
| 1350 |
} |
| 1351 |
if ((*rcode >= '2') && (*rcode <= '5') && (fname != ndx_str)) { |
| 1352 |
while (*fname == ' ') { |
| 1353 |
fname++; /* trim leading spaces */ |
| 1354 |
} |
| 1355 |
for (fvalue = fname; *fvalue; fvalue++) { |
| 1356 |
if (*fvalue == ':') { |
| 1357 |
*fvalue++ = '\0'; |
| 1358 |
i = (int) strlen(fname) - 1; |
| 1359 |
while (i >= 0 && fname[i] == ' ') { |
| 1360 |
fname[i--] = '\0'; /* trim trailing */ |
| 1361 |
} |
| 1362 |
break; |
| 1363 |
} |
| 1364 |
} |
| 1365 |
if (fvalue) { |
| 1366 |
while (*fvalue == ' ') { |
| 1367 |
fvalue++; /* trim leading spaces */ |
| 1368 |
} |
| 1369 |
} |
| 1370 |
if (*fname) { |
| 1371 |
for (fld = CSOfields; fld; fld = fld->next) { |
| 1372 |
if (!strcmp(fld->name, fname)) { |
| 1373 |
if (fld->description) { |
| 1374 |
fname = fld->description; |
| 1375 |
} |
| 1376 |
break; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
if (fld && fld->url) { |
| 1380 |
HTSprintf0(&buf, |
| 1381 |
"<DT><I>%s</I><DD><A HREF=\"%s\">%s</A>\n", |
| 1382 |
fname, fvalue, fvalue); |
| 1383 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1384 |
} else { |
| 1385 |
HTSprintf0(&buf, "<DT><I>%s</I><DD>", fname); |
| 1386 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1387 |
buf[0] = '\0'; |
| 1388 |
l = fvalue; |
| 1389 |
while (*l) { |
| 1390 |
if (*l == '<') { |
| 1391 |
StrAllocCat(buf, "<"); |
| 1392 |
l++; |
| 1393 |
} else if (*l == '>') { |
| 1394 |
StrAllocCat(buf, ">"); |
| 1395 |
l++; |
| 1396 |
} else if (StrNCmp(l, STR_NEWS_URL, LEN_NEWS_URL) && |
| 1397 |
StrNCmp(l, "snews://", 8) && |
| 1398 |
StrNCmp(l, "nntp://", 7) && |
| 1399 |
StrNCmp(l, "snewspost:", 10) && |
| 1400 |
StrNCmp(l, "snewsreply:", 11) && |
| 1401 |
StrNCmp(l, "newspost:", 9) && |
| 1402 |
StrNCmp(l, "newsreply:", 10) && |
| 1403 |
StrNCmp(l, "ftp://", 6) && |
| 1404 |
StrNCmp(l, "file:/", 6) && |
| 1405 |
StrNCmp(l, "finger://", 9) && |
| 1406 |
StrNCmp(l, "http://", 7) && |
| 1407 |
StrNCmp(l, "https://", 8) && |
| 1408 |
StrNCmp(l, "wais://", 7) && |
| 1409 |
StrNCmp(l, STR_MAILTO_URL, |
| 1410 |
LEN_MAILTO_URL) && |
| 1411 |
StrNCmp(l, "cso://", 6) && |
| 1412 |
StrNCmp(l, "gopher://", 9)) { |
| 1413 |
HTSprintf(&buf, "%c", *l++); |
| 1414 |
} else { |
| 1415 |
StrAllocCat(buf, "<a href=\""); |
| 1416 |
StrAllocCopy(href, l); |
| 1417 |
StrAllocCat(buf, strtok(href, " \r\n\t,>)\"")); |
| 1418 |
StrAllocCat(buf, "\">"); |
| 1419 |
while (*l && !StrChr(" \r\n\t,>)\"", *l)) { |
| 1420 |
HTSprintf(&buf, "%c", *l++); |
| 1421 |
} |
| 1422 |
StrAllocCat(buf, "</a>"); |
| 1423 |
FREE(href); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
StrAllocCat(buf, "\n"); |
| 1427 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1428 |
} |
| 1429 |
} else { |
| 1430 |
HTSprintf0(&buf, "<DD>"); |
| 1431 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1432 |
buf[0] = '\0'; |
| 1433 |
l = fvalue; |
| 1434 |
while (*l) { |
| 1435 |
if (*l == '<') { |
| 1436 |
StrAllocCat(buf, "<"); |
| 1437 |
l++; |
| 1438 |
} else if (*l == '>') { |
| 1439 |
StrAllocCat(buf, ">"); |
| 1440 |
l++; |
| 1441 |
} else if (StrNCmp(l, STR_NEWS_URL, LEN_NEWS_URL) && |
| 1442 |
StrNCmp(l, "snews://", 8) && |
| 1443 |
StrNCmp(l, "nntp://", 7) && |
| 1444 |
StrNCmp(l, "snewspost:", 10) && |
| 1445 |
StrNCmp(l, "snewsreply:", 11) && |
| 1446 |
StrNCmp(l, "newspost:", 9) && |
| 1447 |
StrNCmp(l, "newsreply:", 10) && |
| 1448 |
StrNCmp(l, "ftp://", 6) && |
| 1449 |
StrNCmp(l, "file:/", 6) && |
| 1450 |
StrNCmp(l, "finger://", 9) && |
| 1451 |
StrNCmp(l, "http://", 7) && |
| 1452 |
StrNCmp(l, "https://", 8) && |
| 1453 |
StrNCmp(l, "wais://", 7) && |
| 1454 |
StrNCmp(l, STR_MAILTO_URL, LEN_MAILTO_URL) && |
| 1455 |
StrNCmp(l, "cso://", 6) && |
| 1456 |
StrNCmp(l, "gopher://", 9)) { |
| 1457 |
HTSprintf(&buf, "%c", *l++); |
| 1458 |
} else { |
| 1459 |
StrAllocCat(buf, "<a href=\""); |
| 1460 |
StrAllocCopy(href, l); |
| 1461 |
StrAllocCat(buf, strtok(href, " \r\n\t,>)\"")); |
| 1462 |
StrAllocCat(buf, "\">"); |
| 1463 |
while (*l && !StrChr(" \r\n\t,>)\"", *l)) { |
| 1464 |
HTSprintf(&buf, "%c", *l++); |
| 1465 |
} |
| 1466 |
StrAllocCat(buf, "</a>"); |
| 1467 |
FREE(href); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
StrAllocCat(buf, "\n"); |
| 1471 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1472 |
} |
| 1473 |
} else { |
| 1474 |
HTSprintf0(&buf, "<DD>%s\n", fname); |
| 1475 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
} |
| 1479 |
end_CSOreport: |
| 1480 |
if (prev_ndx != -100) { |
| 1481 |
HTSprintf0(&buf, "</DL></DL>\n"); |
| 1482 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1483 |
} |
| 1484 |
FREE(buf); |
| 1485 |
return 0; |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* CSO/PH form-based search gateway - FM HTLoadCSO |
| 1489 |
* ===================================== |
| 1490 |
*/ |
| 1491 |
static int HTLoadCSO(const char *arg, |
| 1492 |
HTParentAnchor *anAnchor, |
| 1493 |
HTFormat format_out, |
| 1494 |
HTStream *sink) |
| 1495 |
{ |
| 1496 |
static const char end_form[] = "</BODY>\n</HTML>\n"; |
| 1497 |
char *host, *cp, *data; |
| 1498 |
int port = CSO_PORT; |
| 1499 |
int status; /* tcp return */ |
| 1500 |
bstring *command = NULL; |
| 1501 |
bstring *content = NULL; |
| 1502 |
int len, i, j, start, finish, flen, ndx; |
| 1503 |
int return_type, has_indexed; |
| 1504 |
CSOfield_info *fld; |
| 1505 |
char buf[2048]; |
| 1506 |
HTFormat format_in = WWW_HTML; |
| 1507 |
HTStream *Target = NULL; |
| 1508 |
|
| 1509 |
if (!acceptable_inited) |
| 1510 |
init_acceptable(); |
| 1511 |
|
| 1512 |
if (!arg) |
| 1513 |
return -3; /* Bad if no name specified */ |
| 1514 |
if (!*arg) |
| 1515 |
return -2; /* Bad if name had zero length */ |
| 1516 |
CTRACE((tfp, "HTLoadCSO: Looking for %s\n", arg)); |
| 1517 |
|
| 1518 |
/* |
| 1519 |
* Set up a socket to the server for the data. |
| 1520 |
*/ |
| 1521 |
status = HTDoConnect(arg, "cso", CSO_PORT, &s); |
| 1522 |
if (status == HT_INTERRUPTED) { |
| 1523 |
/* |
| 1524 |
* Interrupt cleanly. |
| 1525 |
*/ |
| 1526 |
CTRACE((tfp, |
| 1527 |
"HTLoadCSO: Interrupted on connect; recovering cleanly.\n")); |
| 1528 |
_HTProgress(CONNECTION_INTERRUPTED); |
| 1529 |
return HT_NOT_LOADED; |
| 1530 |
} |
| 1531 |
if (status < 0) { |
| 1532 |
CTRACE((tfp, "HTLoadCSO: Unable to connect to remote host for `%s'.\n", |
| 1533 |
arg)); |
| 1534 |
return HTInetStatus("connect"); |
| 1535 |
} |
| 1536 |
|
| 1537 |
HTInitInput(s); /* Set up input buffering */ |
| 1538 |
|
| 1539 |
HTBprintf(&command, "fields%c%c", CR, LF); |
| 1540 |
if (TRACE) { |
| 1541 |
CTRACE((tfp, "HTLoadCSO: Connected, writing command `")); |
| 1542 |
trace_bstring(command); |
| 1543 |
CTRACE((tfp, "' to socket %d\n", s)); |
| 1544 |
} |
| 1545 |
_HTProgress(GOPHER_SENDING_CSO_REQUEST); |
| 1546 |
status = (int) NETWRITE(s, BStrData(command), BStrLen(command)); |
| 1547 |
BStrFree(command); |
| 1548 |
if (status < 0) { |
| 1549 |
CTRACE((tfp, "HTLoadCSO: Unable to send command.\n")); |
| 1550 |
return HTInetStatus("send"); |
| 1551 |
} |
| 1552 |
_HTProgress(GOPHER_SENT_CSO_REQUEST); |
| 1553 |
|
| 1554 |
/* |
| 1555 |
* Now read the data from the socket. |
| 1556 |
*/ |
| 1557 |
status = parse_cso_fields(buf, sizeof(buf)); |
| 1558 |
if (status) { |
| 1559 |
NETCLOSE(s); |
| 1560 |
if (status == HT_INTERRUPTED) { |
| 1561 |
_HTProgress(CONNECTION_INTERRUPTED); |
| 1562 |
} else if (buf[0] != '\0') { |
| 1563 |
HTAlert(buf); |
| 1564 |
} else { |
| 1565 |
HTAlert(FAILED_NO_RESPONSE); |
| 1566 |
} |
| 1567 |
return HT_NOT_LOADED; |
| 1568 |
} |
| 1569 |
Target = HTStreamStack(format_in, |
| 1570 |
format_out, |
| 1571 |
sink, anAnchor); |
| 1572 |
if (Target == NULL) { |
| 1573 |
char *temp = NULL; |
| 1574 |
|
| 1575 |
HTSprintf0(&temp, CANNOT_CONVERT_I_TO_O, |
| 1576 |
HTAtom_name(format_in), HTAtom_name(format_out)); |
| 1577 |
HTAlert(temp); |
| 1578 |
FREE(temp); |
| 1579 |
NETCLOSE(s); |
| 1580 |
return HT_NOT_LOADED; |
| 1581 |
} |
| 1582 |
host = HTParse(arg, "", PARSE_HOST); |
| 1583 |
if ((cp = HTParsePort(host, &port)) != NULL) { |
| 1584 |
if (port == CSO_PORT) { |
| 1585 |
*cp = '\0'; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
anAnchor->safe = TRUE; |
| 1589 |
if (isBEmpty(anAnchor->post_data)) { |
| 1590 |
generate_cso_form(host, port, buf, sizeof(buf), Target); |
| 1591 |
(*Target->isa->_free) (Target); |
| 1592 |
FREE(host); |
| 1593 |
NETCLOSE(s); |
| 1594 |
free_CSOfields(); |
| 1595 |
return HT_LOADED; |
| 1596 |
} |
| 1597 |
|
| 1598 |
HTBprintf(&command, |
| 1599 |
"<HTML>\n<HEAD>\n<TITLE>CSO/PH Results on %s</TITLE>\n</HEAD>\n<BODY>\n", |
| 1600 |
host); |
| 1601 |
(*Target->isa->put_block) (Target, BStrData(command), BStrLen(command)); |
| 1602 |
BStrFree(command); |
| 1603 |
FREE(host); |
| 1604 |
|
| 1605 |
BStrCopy(content, anAnchor->post_data); |
| 1606 |
assert(content != NULL); |
| 1607 |
|
| 1608 |
if (BStrData(content)[BStrLen(content) - 1] != '&') |
| 1609 |
BStrCat0(content, "&"); |
| 1610 |
|
| 1611 |
data = BStrData(content); |
| 1612 |
len = BStrLen(content); |
| 1613 |
for (i = 0; i < len; i++) { |
| 1614 |
if (data[i] == '+') { |
| 1615 |
data[i] = ' '; |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
data = BStrData(content); |
| 1620 |
HTUnEscape(data); /* FIXME: could it have embedded null? */ |
| 1621 |
len = BStrLen(content); |
| 1622 |
|
| 1623 |
return_type = 0; |
| 1624 |
has_indexed = 0; |
| 1625 |
start = 0; |
| 1626 |
for (i = 0; i < len; i++) { |
| 1627 |
if (!data[i] || data[i] == '&') { |
| 1628 |
/* |
| 1629 |
* Value parsed. Unescape characters and look for first '=' to |
| 1630 |
* delimit field name from value. |
| 1631 |
*/ |
| 1632 |
flen = i - start; |
| 1633 |
finish = start + flen; |
| 1634 |
data[finish] = '\0'; |
| 1635 |
for (j = start; j < finish; j++) { |
| 1636 |
if (data[j] == '=') { |
| 1637 |
/* |
| 1638 |
* data[start..j-1] is field name, |
| 1639 |
* [j+1..finish-1] is value. |
| 1640 |
*/ |
| 1641 |
if ((data[start + 1] == '_') && |
| 1642 |
((data[start] == 'r') || (data[start] == 'q'))) { |
| 1643 |
/* |
| 1644 |
* Decode fields number and lookup field info. |
| 1645 |
*/ |
| 1646 |
sscanf(&data[start + 2], "%d=", &ndx); |
| 1647 |
for (fld = CSOfields; fld; fld = fld->next) { |
| 1648 |
if (ndx == fld->id) { |
| 1649 |
if ((j + 1) >= finish) |
| 1650 |
break; /* ignore nulls */ |
| 1651 |
if (data[start] == 'q') { |
| 1652 |
/* |
| 1653 |
* Append field to query line. |
| 1654 |
*/ |
| 1655 |
if (fld->lookup) { |
| 1656 |
if (fld->indexed) |
| 1657 |
has_indexed = 1; |
| 1658 |
if (isBEmpty(command)) { |
| 1659 |
BStrCopy0(command, "query "); |
| 1660 |
} else { |
| 1661 |
BStrCat0(command, " "); |
| 1662 |
} |
| 1663 |
HTBprintf(&command, "%s=\"%s\"", |
| 1664 |
fld->name, &data[j + 1]); |
| 1665 |
} else { |
| 1666 |
strcpy(buf, |
| 1667 |
"Warning: non-lookup field ignored<BR>\n"); |
| 1668 |
(*Target->isa->put_block) (Target, |
| 1669 |
buf, |
| 1670 |
(int) |
| 1671 |
strlen(buf)); |
| 1672 |
} |
| 1673 |
} else if (data[start] == 'r') { |
| 1674 |
fld->explicit_return = 1; |
| 1675 |
} |
| 1676 |
break; |
| 1677 |
} |
| 1678 |
} |
| 1679 |
} else if (!StrNCmp(&data[start], "return=", 7)) { |
| 1680 |
if (!strcmp(&data[start + 7], "all")) { |
| 1681 |
return_type = 1; |
| 1682 |
} else if (!strcmp(&data[start + 7], "selected")) { |
| 1683 |
return_type = 2; |
| 1684 |
} |
| 1685 |
} |
| 1686 |
} |
| 1687 |
} |
| 1688 |
start = i + 1; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
BStrFree(content); |
| 1692 |
if (isBEmpty(command) || !has_indexed) { |
| 1693 |
NETCLOSE(s); |
| 1694 |
strcpy(buf, |
| 1695 |
"<EM>Error:</EM> At least one indexed field value must be specified!\n"); |
| 1696 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1697 |
strcpy(buf, "</BODY>\n</HTML>\n"); |
| 1698 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1699 |
(*Target->isa->_free) (Target); |
| 1700 |
free_CSOfields(); |
| 1701 |
BStrFree(command); |
| 1702 |
return HT_LOADED; |
| 1703 |
} |
| 1704 |
/* |
| 1705 |
* Append return fields. |
| 1706 |
*/ |
| 1707 |
if (return_type == 1) { |
| 1708 |
BStrCat0(command, " return all"); |
| 1709 |
} else if (return_type == 2) { |
| 1710 |
BStrCat0(command, " return"); |
| 1711 |
for (fld = CSOfields; fld; fld = fld->next) { |
| 1712 |
if (fld->explicit_return) { |
| 1713 |
HTBprintf(&command, " %s", fld->name); |
| 1714 |
} |
| 1715 |
} |
| 1716 |
} |
| 1717 |
HTBprintf(&command, "%c%c", CR, LF); |
| 1718 |
strcpy(buf, "<H2>\n<EM>CSO/PH command:</EM> "); |
| 1719 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1720 |
(*Target->isa->put_block) (Target, BStrData(command), BStrLen(command)); |
| 1721 |
strcpy(buf, "</H2>\n"); |
| 1722 |
(*Target->isa->put_block) (Target, buf, (int) strlen(buf)); |
| 1723 |
if (TRACE) { |
| 1724 |
CTRACE((tfp, "HTLoadCSO: Writing command `")); |
| 1725 |
trace_bstring(command); |
| 1726 |
CTRACE((tfp, "' to socket %d\n", s)); |
| 1727 |
} |
| 1728 |
status = (int) NETWRITE(s, BStrData(command), BStrLen(command)); |
| 1729 |
BStrFree(command); |
| 1730 |
if (status < 0) { |
| 1731 |
CTRACE((tfp, "HTLoadCSO: Unable to send command.\n")); |
| 1732 |
free_CSOfields(); |
| 1733 |
return HTInetStatus("send"); |
| 1734 |
} |
| 1735 |
generate_cso_report(Target); |
| 1736 |
NETCLOSE(s); |
| 1737 |
(*Target->isa->put_block) (Target, end_form, (int) sizeof(end_form) - 1); |
| 1738 |
(*Target->isa->_free) (Target); |
| 1739 |
FREE(host); |
| 1740 |
free_CSOfields(); |
| 1741 |
return HT_LOADED; |
| 1742 |
} |
| 1743 |
|
| 1744 |
static char *link_to_URL(const char *arg) |
| 1745 |
{ |
| 1746 |
char *result; |
| 1747 |
char *next; |
| 1748 |
char *temp = NULL; |
| 1749 |
|
| 1750 |
StrAllocCopy(temp, arg); |
| 1751 |
HTUnEscape(temp); |
| 1752 |
result = temp; |
| 1753 |
|
| 1754 |
/* skip past method://host */ |
| 1755 |
if ((next = strstr(result, "://")) != NULL) { |
| 1756 |
result = next + 3; |
| 1757 |
} |
| 1758 |
if ((next = strchr(result, '/')) != NULL) { |
| 1759 |
result = next + 1; |
| 1760 |
} |
| 1761 |
/* check if the selector is the special html one */ |
| 1762 |
if (!strncmp(result, "hURL:", (size_t) 5)) { |
| 1763 |
result += 5; |
| 1764 |
next = result; |
| 1765 |
result = temp; |
| 1766 |
while ((*temp++ = *next++) != 0) ; |
| 1767 |
} else { |
| 1768 |
FREE(temp); |
| 1769 |
result = NULL; |
| 1770 |
} |
| 1771 |
return result; |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* Load by name. HTLoadGopher |
| 1775 |
* ============= |
| 1776 |
* |
| 1777 |
*/ |
| 1778 |
static int HTLoadGopher(const char *arg, |
| 1779 |
HTParentAnchor *anAnchor, |
| 1780 |
HTFormat format_out, |
| 1781 |
HTStream *sink) |
| 1782 |
{ |
| 1783 |
char *hURL; |
| 1784 |
char *command; /* The whole command */ |
| 1785 |
int status; /* tcp return */ |
| 1786 |
char gtype; /* Gopher Node type */ |
| 1787 |
char *selector; /* Selector string */ |
| 1788 |
|
| 1789 |
if (!acceptable_inited) |
| 1790 |
init_acceptable(); |
| 1791 |
|
| 1792 |
if (!arg) |
| 1793 |
return -3; /* Bad if no name specified */ |
| 1794 |
if (!*arg) |
| 1795 |
return -2; /* Bad if name had zero length */ |
| 1796 |
CTRACE((tfp, "HTGopher: Looking for %s\n", arg)); |
| 1797 |
|
| 1798 |
/* |
| 1799 |
* If it's a port 105 GOPHER_CSO gtype with no ISINDEX token ('?'), use the |
| 1800 |
* form-based CSO gateway (otherwise, return an ISINDEX cover page or do |
| 1801 |
* the ISINDEX search). - FM |
| 1802 |
*/ |
| 1803 |
{ |
| 1804 |
size_t len; |
| 1805 |
|
| 1806 |
if ((len = strlen(arg)) > 5) { |
| 1807 |
if (0 == strcmp((const char *) &arg[len - 6], ":105/2")) { |
| 1808 |
/* Use CSO gateway. */ |
| 1809 |
CTRACE((tfp, "HTGopher: Passing to CSO/PH gateway.\n")); |
| 1810 |
return HTLoadCSO(arg, anAnchor, format_out, sink); |
| 1811 |
} |
| 1812 |
} |
| 1813 |
} |
| 1814 |
|
| 1815 |
/* |
| 1816 |
* If it's a port 79/0[/...] URL, use the finger gateway. - FM |
| 1817 |
*/ |
| 1818 |
if (strstr(arg, ":79/0") != NULL) { |
| 1819 |
#ifndef DISABLE_FINGER |
| 1820 |
CTRACE((tfp, "HTGopher: Passing to finger gateway.\n")); |
| 1821 |
return HTLoadFinger(arg, anAnchor, format_out, sink); |
| 1822 |
#else /* finger is disabled */ |
| 1823 |
HTAlert(COULD_NOT_ACCESS_DOCUMENT); |
| 1824 |
return HT_NOT_LOADED; |
| 1825 |
#endif /* DISABLE_FINGER */ |
| 1826 |
} |
| 1827 |
|
| 1828 |
/* |
| 1829 |
* Get entity type, and selector string. |
| 1830 |
*/ |
| 1831 |
{ |
| 1832 |
char *p1 = HTParse(arg, "", PARSE_PATH | PARSE_PUNCTUATION); |
| 1833 |
|
| 1834 |
gtype = '1'; /* Default = menu */ |
| 1835 |
selector = p1; |
| 1836 |
if ((*selector++ == '/') && (*selector)) { /* Skip first slash */ |
| 1837 |
gtype = *selector++; /* Pick up gtype */ |
| 1838 |
} |
| 1839 |
if (gtype == GOPHER_INDEX) { |
| 1840 |
char *query; |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Search is allowed. |
| 1844 |
*/ |
| 1845 |
HTAnchor_setIndex(anAnchor, anAnchor->address); |
| 1846 |
query = StrChr(selector, '?'); /* Look for search string */ |
| 1847 |
if (!query || !query[1]) { /* No search required */ |
| 1848 |
target = HTML_new(anAnchor, format_out, sink); |
| 1849 |
targetClass = *target->isa; |
| 1850 |
display_index(arg, anAnchor); /* Display "cover page" */ |
| 1851 |
return HT_LOADED; /* Local function only */ |
| 1852 |
} |
| 1853 |
*query++ = '\0'; /* Skip '?' */ |
| 1854 |
command = |
| 1855 |
(char *) malloc(strlen(selector) + 1 + strlen(query) + 2 + 1); |
| 1856 |
if (command == NULL) |
| 1857 |
outofmem(__FILE__, "HTLoadGopher"); |
| 1858 |
|
| 1859 |
de_escape(command, selector); /* Bug fix TBL 921208 */ |
| 1860 |
|
| 1861 |
strcat(command, "\t"); |
| 1862 |
|
| 1863 |
{ /* Remove plus signs 921006 */ |
| 1864 |
char *p; |
| 1865 |
|
| 1866 |
for (p = query; *p; p++) { |
| 1867 |
if (*p == '+') |
| 1868 |
*p = ' '; |
| 1869 |
} |
| 1870 |
} |
| 1871 |
|
| 1872 |
de_escape(&command[strlen(command)], query); /* bug fix LJM 940415 */ |
| 1873 |
} else if (gtype == GOPHER_CSO) { |
| 1874 |
char *query; |
| 1875 |
|
| 1876 |
/* |
| 1877 |
* Search is allowed. |
| 1878 |
*/ |
| 1879 |
query = StrChr(selector, '?'); /* Look for search string */ |
| 1880 |
if (!query || !query[1]) { /* No search required */ |
| 1881 |
target = HTML_new(anAnchor, format_out, sink); |
| 1882 |
targetClass = *target->isa; |
| 1883 |
display_cso(arg, anAnchor); /* Display "cover page" */ |
| 1884 |
return HT_LOADED; /* Local function only */ |
| 1885 |
} |
| 1886 |
HTAnchor_setIndex(anAnchor, anAnchor->address); |
| 1887 |
*query++ = '\0'; /* Skip '?' */ |
| 1888 |
command = (char *) malloc(strlen("query") + 1 + |
| 1889 |
strlen(query) + 2 + 1); |
| 1890 |
if (command == NULL) |
| 1891 |
outofmem(__FILE__, "HTLoadGopher"); |
| 1892 |
|
| 1893 |
de_escape(command, selector); /* Bug fix TBL 921208 */ |
| 1894 |
|
| 1895 |
strcpy(command, "query "); |
| 1896 |
|
| 1897 |
{ /* Remove plus signs 921006 */ |
| 1898 |
char *p; |
| 1899 |
|
| 1900 |
for (p = query; *p; p++) { |
| 1901 |
if (*p == '+') |
| 1902 |
*p = ' '; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
de_escape(&command[strlen(command)], query); /* bug fix LJM 940415 */ |
| 1906 |
|
| 1907 |
} else { /* Not index */ |
| 1908 |
command = (char *) malloc(strlen(selector) + 2 + 1); |
| 1909 |
if (command == NULL) |
| 1910 |
outofmem(__FILE__, "HTLoadGopher"); |
| 1911 |
|
| 1912 |
de_escape(command, selector); |
| 1913 |
} |
| 1914 |
FREE(p1); |
| 1915 |
} |
| 1916 |
|
| 1917 |
{ |
| 1918 |
char *p = command + strlen(command); |
| 1919 |
|
| 1920 |
*p++ = CR; /* Macros to be correct on Mac */ |
| 1921 |
*p++ = LF; |
| 1922 |
*p = '\0'; |
| 1923 |
} |
| 1924 |
/* |
| 1925 |
* Check for link to URL |
| 1926 |
*/ |
| 1927 |
if ((hURL = link_to_URL(arg)) != NULL) { |
| 1928 |
CTRACE((tfp, "gopher found link to URL '%s'\n", hURL)); |
| 1929 |
free(hURL); |
| 1930 |
} |
| 1931 |
|
| 1932 |
/* |
| 1933 |
* Set up a socket to the server for the data. |
| 1934 |
*/ |
| 1935 |
status = HTDoConnect(arg, "gopher", GOPHER_PORT, &s); |
| 1936 |
if (status == HT_INTERRUPTED) { |
| 1937 |
/* |
| 1938 |
* Interrupt cleanly. |
| 1939 |
*/ |
| 1940 |
CTRACE((tfp, "HTGopher: Interrupted on connect; recovering cleanly.\n")); |
| 1941 |
_HTProgress(CONNECTION_INTERRUPTED); |
| 1942 |
FREE(command); |
| 1943 |
return HT_NOT_LOADED; |
| 1944 |
} |
| 1945 |
if (status < 0) { |
| 1946 |
CTRACE((tfp, "HTGopher: Unable to connect to remote host for `%s'.\n", |
| 1947 |
arg)); |
| 1948 |
FREE(command); |
| 1949 |
return HTInetStatus("connect"); |
| 1950 |
} |
| 1951 |
|
| 1952 |
HTInitInput(s); /* Set up input buffering */ |
| 1953 |
|
| 1954 |
CTRACE((tfp, "HTGopher: Connected, writing command `%s' to socket %d\n", |
| 1955 |
command, s)); |
| 1956 |
|
| 1957 |
#ifdef NOT_ASCII |
| 1958 |
{ |
| 1959 |
char *p; |
| 1960 |
|
| 1961 |
for (p = command; *p; p++) { |
| 1962 |
*p = TOASCII(*p); |
| 1963 |
} |
| 1964 |
} |
| 1965 |
#endif |
| 1966 |
|
| 1967 |
_HTProgress(GOPHER_SENDING_REQUEST); |
| 1968 |
|
| 1969 |
status = (int) NETWRITE(s, command, (int) strlen(command)); |
| 1970 |
FREE(command); |
| 1971 |
if (status < 0) { |
| 1972 |
CTRACE((tfp, "HTGopher: Unable to send command.\n")); |
| 1973 |
return HTInetStatus("send"); |
| 1974 |
} |
| 1975 |
|
| 1976 |
_HTProgress(GOPHER_SENT_REQUEST); |
| 1977 |
|
| 1978 |
/* |
| 1979 |
* Now read the data from the socket. |
| 1980 |
*/ |
| 1981 |
switch (gtype) { |
| 1982 |
|
| 1983 |
case GOPHER_TEXT: |
| 1984 |
HTParseSocket(WWW_PLAINTEXT, format_out, anAnchor, s, sink); |
| 1985 |
break; |
| 1986 |
|
| 1987 |
case GOPHER_HTML: |
| 1988 |
case GOPHER_CHTML: |
| 1989 |
HTParseSocket(WWW_HTML, format_out, anAnchor, s, sink); |
| 1990 |
break; |
| 1991 |
|
| 1992 |
case GOPHER_GIF: |
| 1993 |
case GOPHER_IMAGE: |
| 1994 |
case GOPHER_PLUS_IMAGE: |
| 1995 |
HTParseSocket(HTAtom_for("image/gif"), |
| 1996 |
format_out, anAnchor, s, sink); |
| 1997 |
break; |
| 1998 |
|
| 1999 |
case GOPHER_MENU: |
| 2000 |
case GOPHER_INDEX: |
| 2001 |
target = HTML_new(anAnchor, format_out, sink); |
| 2002 |
targetClass = *target->isa; |
| 2003 |
parse_menu(arg, anAnchor); |
| 2004 |
break; |
| 2005 |
|
| 2006 |
case GOPHER_CSO: |
| 2007 |
target = HTML_new(anAnchor, format_out, sink); |
| 2008 |
targetClass = *target->isa; |
| 2009 |
parse_cso(arg, anAnchor); |
| 2010 |
break; |
| 2011 |
|
| 2012 |
case GOPHER_SOUND: |
| 2013 |
case GOPHER_PLUS_SOUND: |
| 2014 |
HTParseSocket(WWW_AUDIO, format_out, anAnchor, s, sink); |
| 2015 |
break; |
| 2016 |
|
| 2017 |
case GOPHER_PLUS_MOVIE: |
| 2018 |
HTParseSocket(HTAtom_for("video/mpeg"), format_out, anAnchor, s, sink); |
| 2019 |
break; |
| 2020 |
|
| 2021 |
case GOPHER_PLUS_PDF: |
| 2022 |
HTParseSocket(HTAtom_for("application/pdf"), format_out, anAnchor, |
| 2023 |
s, sink); |
| 2024 |
break; |
| 2025 |
|
| 2026 |
default: |
| 2027 |
{ |
| 2028 |
HTAtom *encoding = NULL; |
| 2029 |
const char *desc = NULL; |
| 2030 |
HTFormat format = HTFileFormat(arg, &encoding, &desc); |
| 2031 |
|
| 2032 |
/* |
| 2033 |
* Ignore WWW_BINARY (since that is returned by HTFileFormat when |
| 2034 |
* it does not have a representation), but otherwise use the |
| 2035 |
* result. |
| 2036 |
*/ |
| 2037 |
if (format != WWW_BINARY) { |
| 2038 |
HTParseSocket(format, format_out, anAnchor, s, sink); |
| 2039 |
break; |
| 2040 |
} |
| 2041 |
} |
| 2042 |
/* FALL-THRU */ |
| 2043 |
|
| 2044 |
case GOPHER_MACBINHEX: |
| 2045 |
case GOPHER_PCBINARY: |
| 2046 |
case GOPHER_UUENCODED: |
| 2047 |
case GOPHER_BINARY: |
| 2048 |
/* |
| 2049 |
* Specifying WWW_UNKNOWN forces dump to local disk. |
| 2050 |
*/ |
| 2051 |
HTParseSocket(WWW_UNKNOWN, format_out, anAnchor, s, sink); |
| 2052 |
break; |
| 2053 |
|
| 2054 |
} /* switch(gtype) */ |
| 2055 |
|
| 2056 |
NETCLOSE(s); |
| 2057 |
return HT_LOADED; |
| 2058 |
} |
| 2059 |
|
| 2060 |
#ifdef GLOBALDEF_IS_MACRO |
| 2061 |
#define _HTGOPHER_C_1_INIT { "gopher", HTLoadGopher, NULL } |
| 2062 |
GLOBALDEF(HTProtocol, HTGopher, _HTGOPHER_C_1_INIT); |
| 2063 |
#define _HTCSO_C_1_INIT { "cso", HTLoadCSO, NULL } |
| 2064 |
GLOBALDEF(HTProtocol, HTCSO, _HTCSO_C_1_INIT); |
| 2065 |
#else |
| 2066 |
GLOBALDEF HTProtocol HTGopher = |
| 2067 |
{"gopher", HTLoadGopher, NULL}; |
| 2068 |
GLOBALDEF HTProtocol HTCSO = |
| 2069 |
{"cso", HTLoadCSO, NULL}; |
| 2070 |
#endif /* GLOBALDEF_IS_MACRO */ |
| 2071 |
|
| 2072 |
#endif /* not DISABLE_GOPHER */ |