| 1 |
/* |
| 2 |
* $LynxId: HTAAUtil.c,v 1.39 2025/07/22 00:36:14 tom Exp $ |
| 3 |
* |
| 4 |
* MODULE HTAAUtil.c |
| 5 |
* COMMON PARTS OF ACCESS AUTHORIZATION MODULE |
| 6 |
* FOR BOTH SERVER AND BROWSER |
| 7 |
* |
| 8 |
* IMPORTANT: |
| 9 |
* Routines in this module use dynamic allocation, but free |
| 10 |
* automatically all the memory reserved by them. |
| 11 |
* |
| 12 |
* Therefore the caller never has to (and never should) |
| 13 |
* free() any object returned by these functions. |
| 14 |
* |
| 15 |
* Therefore also all the strings returned by this package |
| 16 |
* are only valid until the next call to the same function |
| 17 |
* is made. This approach is selected, because of the nature |
| 18 |
* of access authorization: no string returned by the package |
| 19 |
* needs to be valid longer than until the next call. |
| 20 |
* |
| 21 |
* This also makes it easy to plug the AA package in: |
| 22 |
* you don't have to ponder whether to free() something |
| 23 |
* here or is it done somewhere else (because it is always |
| 24 |
* done somewhere else). |
| 25 |
* |
| 26 |
* The strings that the package needs to store are copied |
| 27 |
* so the original strings given as parameters to AA |
| 28 |
* functions may be freed or modified with no side effects. |
| 29 |
* |
| 30 |
* The AA package does not free() anything else than what |
| 31 |
* it has itself allocated. |
| 32 |
* |
| 33 |
* AA (Access Authorization) package means modules which |
| 34 |
* names start with HTAA. |
| 35 |
* |
| 36 |
* AUTHORS: |
| 37 |
* AL Ari Luotonen [email protected] |
| 38 |
* MD Mark Donszelmann [email protected] |
| 39 |
* |
| 40 |
* HISTORY: |
| 41 |
* 8 Nov 93 MD (VMS only) Added case insensitive comparison in HTAA_templateCaseMatch |
| 42 |
* |
| 43 |
* |
| 44 |
* BUGS: |
| 45 |
* |
| 46 |
* |
| 47 |
*/ |
| 48 |
|
| 49 |
#include <HTTLS.h> |
| 50 |
|
| 51 |
#include <HTAAUtil.h> /* Implemented here */ |
| 52 |
#include <HTAssoc.h> /* Assoc list */ |
| 53 |
#include <HTTCP.h> |
| 54 |
|
| 55 |
#include <LYStrings.h> |
| 56 |
#include <LYUtils.h> |
| 57 |
#include <LYLeaks.h> |
| 58 |
|
| 59 |
/* PUBLIC HTAAScheme_enum() |
| 60 |
* TRANSLATE SCHEME NAME INTO |
| 61 |
* A SCHEME ENUMERATION |
| 62 |
* |
| 63 |
* ON ENTRY: |
| 64 |
* name is a string representing the scheme name. |
| 65 |
* |
| 66 |
* ON EXIT: |
| 67 |
* returns the enumerated constant for that scheme. |
| 68 |
*/ |
| 69 |
HTAAScheme HTAAScheme_enum(const char *name) |
| 70 |
{ |
| 71 |
char *upcased = NULL; |
| 72 |
|
| 73 |
if (!name) |
| 74 |
return HTAA_UNKNOWN; |
| 75 |
|
| 76 |
StrAllocCopy(upcased, name); |
| 77 |
LYUpperCase(upcased); |
| 78 |
|
| 79 |
if (!StrNCmp(upcased, "NONE", 4)) { |
| 80 |
FREE(upcased); |
| 81 |
return HTAA_NONE; |
| 82 |
} else if (!StrNCmp(upcased, "BASIC", 5)) { |
| 83 |
FREE(upcased); |
| 84 |
return HTAA_BASIC; |
| 85 |
} else if (!StrNCmp(upcased, "PUBKEY", 6)) { |
| 86 |
FREE(upcased); |
| 87 |
return HTAA_PUBKEY; |
| 88 |
} else if (!StrNCmp(upcased, "KERBEROSV4", 10)) { |
| 89 |
FREE(upcased); |
| 90 |
return HTAA_KERBEROS_V4; |
| 91 |
} else if (!StrNCmp(upcased, "KERBEROSV5", 10)) { |
| 92 |
FREE(upcased); |
| 93 |
return HTAA_KERBEROS_V5; |
| 94 |
} else { |
| 95 |
FREE(upcased); |
| 96 |
return HTAA_UNKNOWN; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/* PUBLIC HTAAScheme_name() |
| 101 |
* GET THE NAME OF A GIVEN SCHEME |
| 102 |
* ON ENTRY: |
| 103 |
* scheme is one of the scheme enum values: |
| 104 |
* HTAA_NONE, HTAA_BASIC, HTAA_PUBKEY, ... |
| 105 |
* |
| 106 |
* ON EXIT: |
| 107 |
* returns the name of the scheme, i.e. |
| 108 |
* "None", "Basic", "Pubkey", ... |
| 109 |
*/ |
| 110 |
const char *HTAAScheme_name(HTAAScheme scheme) |
| 111 |
{ |
| 112 |
switch (scheme) { |
| 113 |
case HTAA_NONE: |
| 114 |
return "None"; |
| 115 |
case HTAA_BASIC: |
| 116 |
return "Basic"; |
| 117 |
case HTAA_PUBKEY: |
| 118 |
return "Pubkey"; |
| 119 |
case HTAA_KERBEROS_V4: |
| 120 |
return "KerberosV4"; |
| 121 |
case HTAA_KERBEROS_V5: |
| 122 |
return "KerberosV5"; |
| 123 |
case HTAA_UNKNOWN: |
| 124 |
return "UNKNOWN"; |
| 125 |
default: |
| 126 |
return "THIS-IS-A-BUG"; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/* PUBLIC HTAAMethod_enum() |
| 131 |
* TRANSLATE METHOD NAME INTO AN ENUMERATED VALUE |
| 132 |
* ON ENTRY: |
| 133 |
* name is the method name to translate. |
| 134 |
* |
| 135 |
* ON EXIT: |
| 136 |
* returns HTAAMethod enumerated value corresponding |
| 137 |
* to the given name. |
| 138 |
*/ |
| 139 |
HTAAMethod HTAAMethod_enum(const char *name) |
| 140 |
{ |
| 141 |
if (!name) |
| 142 |
return METHOD_UNKNOWN; |
| 143 |
|
| 144 |
if (0 == strcasecomp(name, "GET")) |
| 145 |
return METHOD_GET; |
| 146 |
else if (0 == strcasecomp(name, "PUT")) |
| 147 |
return METHOD_PUT; |
| 148 |
else |
| 149 |
return METHOD_UNKNOWN; |
| 150 |
} |
| 151 |
|
| 152 |
/* PUBLIC HTAAMethod_name() |
| 153 |
* GET THE NAME OF A GIVEN METHOD |
| 154 |
* ON ENTRY: |
| 155 |
* method is one of the method enum values: |
| 156 |
* METHOD_GET, METHOD_PUT, ... |
| 157 |
* |
| 158 |
* ON EXIT: |
| 159 |
* returns the name of the scheme, i.e. |
| 160 |
* "GET", "PUT", ... |
| 161 |
*/ |
| 162 |
const char *HTAAMethod_name(HTAAMethod method) |
| 163 |
{ |
| 164 |
switch (method) { |
| 165 |
case METHOD_GET: |
| 166 |
return "GET"; |
| 167 |
case METHOD_PUT: |
| 168 |
return "PUT"; |
| 169 |
case METHOD_UNKNOWN: |
| 170 |
return "UNKNOWN"; |
| 171 |
default: |
| 172 |
return "THIS-IS-A-BUG"; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* PUBLIC HTAAMethod_inList() |
| 177 |
* IS A METHOD IN A LIST OF METHOD NAMES |
| 178 |
* ON ENTRY: |
| 179 |
* method is the method to look for. |
| 180 |
* list is a list of method names. |
| 181 |
* |
| 182 |
* ON EXIT: |
| 183 |
* returns YES, if method was found. |
| 184 |
* NO, if not found. |
| 185 |
*/ |
| 186 |
BOOL HTAAMethod_inList(HTAAMethod method, HTList *list) |
| 187 |
{ |
| 188 |
HTList *cur = list; |
| 189 |
char *item; |
| 190 |
|
| 191 |
while (NULL != (item = (char *) HTList_nextObject(cur))) { |
| 192 |
CTRACE((tfp, " %s", item)); |
| 193 |
if (method == HTAAMethod_enum(item)) |
| 194 |
return YES; |
| 195 |
} |
| 196 |
|
| 197 |
return NO; /* Not found */ |
| 198 |
} |
| 199 |
|
| 200 |
/* PUBLIC HTAA_templateMatch() |
| 201 |
* STRING COMPARISON FUNCTION FOR FILE NAMES |
| 202 |
* WITH ONE WILDCARD * IN THE TEMPLATE |
| 203 |
* NOTE: |
| 204 |
* This is essentially the same code as in HTRules.c, but it |
| 205 |
* cannot be used because it is embedded in between other code. |
| 206 |
* (In fact, HTRules.c should use this routine, but then this |
| 207 |
* routine would have to be more sophisticated... why is life |
| 208 |
* sometimes so hard...) |
| 209 |
* |
| 210 |
* ON ENTRY: |
| 211 |
* ctemplate is a template string to match the file name |
| 212 |
* against, may contain a single wildcard |
| 213 |
* character * which matches zero or more |
| 214 |
* arbitrary characters. |
| 215 |
* filename is the filename (or pathname) to be matched |
| 216 |
* against the template. |
| 217 |
* |
| 218 |
* ON EXIT: |
| 219 |
* returns YES, if filename matches the template. |
| 220 |
* NO, otherwise. |
| 221 |
*/ |
| 222 |
BOOL HTAA_templateMatch(const char *ctemplate, |
| 223 |
const char *filename) |
| 224 |
{ |
| 225 |
const char *p = ctemplate; |
| 226 |
const char *q = filename; |
| 227 |
int m; |
| 228 |
|
| 229 |
for (; *p && *q && *p == *q; p++, q++) /* Find first mismatch */ |
| 230 |
; /* do nothing else */ |
| 231 |
|
| 232 |
if (!*p && !*q) |
| 233 |
return YES; /* Equally long equal strings */ |
| 234 |
else if ('*' == *p) { /* Wildcard */ |
| 235 |
p++; /* Skip wildcard character */ |
| 236 |
m = (int) (strlen(q) - strlen(p)); /* Amount to match to wildcard */ |
| 237 |
if (m < 0) |
| 238 |
return NO; /* No match, filename too short */ |
| 239 |
else { /* Skip the matched characters and compare */ |
| 240 |
if (strcmp(p, q + m)) |
| 241 |
return NO; /* Tail mismatch */ |
| 242 |
else |
| 243 |
return YES; /* Tail match */ |
| 244 |
} |
| 245 |
/* if wildcard */ |
| 246 |
} else |
| 247 |
return NO; /* Length or character mismatch */ |
| 248 |
} |
| 249 |
|
| 250 |
/* PUBLIC HTAA_templateCaseMatch() |
| 251 |
* STRING COMPARISON FUNCTION FOR FILE NAMES |
| 252 |
* WITH ONE WILDCARD * IN THE TEMPLATE (Case Insensitive) |
| 253 |
* NOTE: |
| 254 |
* This is essentially the same code as in HTAA_templateMatch, but |
| 255 |
* it compares case insensitive (for VMS). Reason for this routine |
| 256 |
* is that HTAA_templateMatch gets called from several places, also |
| 257 |
* there where a case sensitive match is needed, so one cannot just |
| 258 |
* change the HTAA_templateMatch routine for VMS. |
| 259 |
* |
| 260 |
* ON ENTRY: |
| 261 |
* template is a template string to match the file name |
| 262 |
* against, may contain a single wildcard |
| 263 |
* character * which matches zero or more |
| 264 |
* arbitrary characters. |
| 265 |
* filename is the filename (or pathname) to be matched |
| 266 |
* against the template. |
| 267 |
* |
| 268 |
* ON EXIT: |
| 269 |
* returns YES, if filename matches the template. |
| 270 |
* NO, otherwise. |
| 271 |
*/ |
| 272 |
BOOL HTAA_templateCaseMatch(const char *ctemplate, |
| 273 |
const char *filename) |
| 274 |
{ |
| 275 |
const char *p = ctemplate; |
| 276 |
const char *q = filename; |
| 277 |
int m; |
| 278 |
|
| 279 |
/* Find first mismatch */ |
| 280 |
for (; *p && *q && TOUPPER(*p) == TOUPPER(*q); p++, q++) ; /* do nothing else */ |
| 281 |
|
| 282 |
if (!*p && !*q) |
| 283 |
return YES; /* Equally long equal strings */ |
| 284 |
else if ('*' == *p) { /* Wildcard */ |
| 285 |
p++; /* Skip wildcard character */ |
| 286 |
m = (int) (strlen(q) - strlen(p)); /* Amount to match to wildcard */ |
| 287 |
if (m < 0) |
| 288 |
return NO; /* No match, filename too short */ |
| 289 |
else { /* Skip the matched characters and compare */ |
| 290 |
if (strcasecomp(p, q + m)) |
| 291 |
return NO; /* Tail mismatch */ |
| 292 |
else |
| 293 |
return YES; /* Tail match */ |
| 294 |
} |
| 295 |
/* if wildcard */ |
| 296 |
} else |
| 297 |
return NO; /* Length or character mismatch */ |
| 298 |
} |
| 299 |
|
| 300 |
/* PUBLIC HTAA_makeProtectionTemplate() |
| 301 |
* CREATE A PROTECTION TEMPLATE FOR THE FILES |
| 302 |
* IN THE SAME DIRECTORY AS THE GIVEN FILE |
| 303 |
* (Used by server if there is no fancier way for |
| 304 |
* it to tell the client, and by browser if server |
| 305 |
* didn't send WWW-ProtectionTemplate: field) |
| 306 |
* ON ENTRY: |
| 307 |
* docname is the document pathname (from URL). |
| 308 |
* |
| 309 |
* ON EXIT: |
| 310 |
* returns a template matching docname, and other files |
| 311 |
* files in that directory. |
| 312 |
* |
| 313 |
* E.g. /foo/bar/x.html => /foo/bar/ * |
| 314 |
* ^ |
| 315 |
* Space only to prevent it from |
| 316 |
* being a comment marker here, |
| 317 |
* there really isn't any space. |
| 318 |
*/ |
| 319 |
char *HTAA_makeProtectionTemplate(const char *docname) |
| 320 |
{ |
| 321 |
char *ctemplate = NULL; |
| 322 |
char *slash = NULL; |
| 323 |
|
| 324 |
if (docname) { |
| 325 |
StrAllocCopy(ctemplate, docname); |
| 326 |
slash = strrchr(ctemplate, '/'); |
| 327 |
if (slash) |
| 328 |
slash++; |
| 329 |
else |
| 330 |
slash = ctemplate; |
| 331 |
*slash = '\0'; |
| 332 |
StrAllocCat(ctemplate, "*"); |
| 333 |
} else |
| 334 |
StrAllocCopy(ctemplate, "*"); |
| 335 |
|
| 336 |
CTRACE((tfp, "make_template: made template `%s' for file `%s'\n", |
| 337 |
ctemplate, docname)); |
| 338 |
|
| 339 |
return ctemplate; |
| 340 |
} |
| 341 |
|
| 342 |
/* |
| 343 |
* Skip leading whitespace from *s forward |
| 344 |
*/ |
| 345 |
#define SKIPWS(s) while (*s==' ' || *s=='\t') s++; |
| 346 |
|
| 347 |
/* |
| 348 |
* Kill trailing whitespace starting from *(s-1) backwards |
| 349 |
*/ |
| 350 |
#define KILLWS(s) {char *c=s-1; while (*c==' ' || *c=='\t') *(c--)='\0';} |
| 351 |
|
| 352 |
/* PUBLIC HTAA_parseArgList() |
| 353 |
* PARSE AN ARGUMENT LIST GIVEN IN A HEADER FIELD |
| 354 |
* ON ENTRY: |
| 355 |
* str is a comma-separated list: |
| 356 |
* |
| 357 |
* item, item, item |
| 358 |
* where |
| 359 |
* item ::= value |
| 360 |
* | name=value |
| 361 |
* | name="value" |
| 362 |
* |
| 363 |
* Leading and trailing whitespace is ignored |
| 364 |
* everywhere except inside quotes, so the following |
| 365 |
* examples are equal: |
| 366 |
* |
| 367 |
* name=value,foo=bar |
| 368 |
* name="value",foo="bar" |
| 369 |
* name = value , foo = bar |
| 370 |
* name = "value" , foo = "bar" |
| 371 |
* |
| 372 |
* ON EXIT: |
| 373 |
* returns a list of name-value pairs (actually HTAssocList*). |
| 374 |
* For items with no name, just value, the name is |
| 375 |
* the number of order number of that item. E.g. |
| 376 |
* "1" for the first, etc. |
| 377 |
*/ |
| 378 |
HTAssocList *HTAA_parseArgList(char *str) |
| 379 |
{ |
| 380 |
HTAssocList *assoc_list = HTAssocList_new(); |
| 381 |
char *cur = NULL; |
| 382 |
char *name = NULL; |
| 383 |
int n = 0; |
| 384 |
|
| 385 |
if (!str) |
| 386 |
return assoc_list; |
| 387 |
|
| 388 |
while (*str) { |
| 389 |
SKIPWS(str); /* Skip leading whitespace */ |
| 390 |
cur = str; |
| 391 |
n++; |
| 392 |
|
| 393 |
while (*cur && *cur != '=' && *cur != ',') |
| 394 |
cur++; /* Find end of name (or lonely value without a name) */ |
| 395 |
KILLWS(cur); /* Kill trailing whitespace */ |
| 396 |
|
| 397 |
if (*cur == '=') { /* Name followed by a value */ |
| 398 |
*(cur++) = '\0'; /* Terminate name */ |
| 399 |
StrAllocCopy(name, str); |
| 400 |
SKIPWS(cur); /* Skip WS leading the value */ |
| 401 |
str = cur; |
| 402 |
if (*str == '"') { /* Quoted value */ |
| 403 |
str++; |
| 404 |
cur = str; |
| 405 |
while (*cur && *cur != '"') |
| 406 |
cur++; |
| 407 |
if (*cur == '"') |
| 408 |
*(cur++) = '\0'; /* Terminate value */ |
| 409 |
/* else it is lacking terminating quote */ |
| 410 |
SKIPWS(cur); /* Skip WS leading comma */ |
| 411 |
if (*cur == ',') |
| 412 |
cur++; /* Skip separating colon */ |
| 413 |
} else { /* Unquoted value */ |
| 414 |
while (*cur && *cur != ',') |
| 415 |
cur++; |
| 416 |
KILLWS(cur); /* Kill trailing whitespace */ |
| 417 |
if (*cur == ',') |
| 418 |
*(cur++) = '\0'; |
| 419 |
/* else *cur already NULL */ |
| 420 |
} |
| 421 |
} else { /* No name, just a value */ |
| 422 |
if (*cur == ',') |
| 423 |
*(cur++) = '\0'; /* Terminate value */ |
| 424 |
/* else last value on line (already terminated by NULL) */ |
| 425 |
HTSprintf0(&name, "%d", n); /* Item order number for name */ |
| 426 |
} |
| 427 |
HTAssocList_add(assoc_list, name, str); |
| 428 |
str = cur; |
| 429 |
} /* while *str */ |
| 430 |
|
| 431 |
FREE(name); |
| 432 |
return assoc_list; |
| 433 |
} |
| 434 |
|
| 435 |
/************** HEADER LINE READER -- DOES UNFOLDING *************************/ |
| 436 |
|
| 437 |
#define BUFFER_SIZE 1024 |
| 438 |
|
| 439 |
static size_t buffer_length; |
| 440 |
static char *buffer = NULL; |
| 441 |
static char *start_pointer; |
| 442 |
static char *end_pointer; |
| 443 |
static int in_soc = -1; |
| 444 |
|
| 445 |
#ifdef LY_FIND_LEAKS |
| 446 |
static void FreeHTAAUtil(void) |
| 447 |
{ |
| 448 |
FREE(buffer); |
| 449 |
} |
| 450 |
#endif /* LY_FIND_LEAKS */ |
| 451 |
|
| 452 |
/* PUBLIC HTAA_setupReader() |
| 453 |
* SET UP HEADER LINE READER, i.e., give |
| 454 |
* the already-read-but-not-yet-processed |
| 455 |
* buffer of text to be read before more |
| 456 |
* is read from the socket. |
| 457 |
* ON ENTRY: |
| 458 |
* start_of_headers is a pointer to a buffer containing |
| 459 |
* the beginning of the header lines |
| 460 |
* (rest will be read from a socket). |
| 461 |
* length is the number of valid characters in |
| 462 |
* 'start_of_headers' buffer. |
| 463 |
* soc is the socket to use when start_of_headers |
| 464 |
* buffer is used up. |
| 465 |
* ON EXIT: |
| 466 |
* returns nothing. |
| 467 |
* Subsequent calls to HTAA_getUnfoldedLine() |
| 468 |
* will use this buffer first and then |
| 469 |
* proceed to read from socket. |
| 470 |
*/ |
| 471 |
void HTAA_setupReader(char *start_of_headers, |
| 472 |
size_t length, |
| 473 |
int soc) |
| 474 |
{ |
| 475 |
if (!start_of_headers) |
| 476 |
length = 0; /* initialize length (is this reached at all?) */ |
| 477 |
if (buffer == NULL) { /* first call? */ |
| 478 |
buffer_length = length; |
| 479 |
if (buffer_length < BUFFER_SIZE) /* would fall below BUFFER_SIZE? */ |
| 480 |
buffer_length = BUFFER_SIZE; |
| 481 |
buffer = (char *) malloc((size_t) (sizeof(char) * (buffer_length + 1))); |
| 482 |
} else if (length > buffer_length) { /* need more space? */ |
| 483 |
buffer_length = length; |
| 484 |
buffer = (char *) realloc((char *) buffer, |
| 485 |
(size_t) (sizeof(char) * (buffer_length + 1))); |
| 486 |
} |
| 487 |
if (buffer == NULL) |
| 488 |
outofmem(__FILE__, "HTAA_setupReader"); |
| 489 |
|
| 490 |
#ifdef LY_FIND_LEAKS |
| 491 |
atexit(FreeHTAAUtil); |
| 492 |
#endif |
| 493 |
start_pointer = buffer; |
| 494 |
if (start_of_headers) { |
| 495 |
LYStrNCpy(buffer, start_of_headers, length); |
| 496 |
end_pointer = buffer + length; |
| 497 |
} else { |
| 498 |
*start_pointer = '\0'; |
| 499 |
end_pointer = start_pointer; |
| 500 |
} |
| 501 |
in_soc = soc; |
| 502 |
} |
| 503 |
|
| 504 |
/* PUBLIC HTAA_getUnfoldedLine() |
| 505 |
* READ AN UNFOLDED HEADER LINE FROM SOCKET |
| 506 |
* ON ENTRY: |
| 507 |
* HTAA_setupReader must absolutely be called before |
| 508 |
* this function to set up internal buffer. |
| 509 |
* |
| 510 |
* ON EXIT: |
| 511 |
* returns a newly-allocated character string representing |
| 512 |
* the read line. The line is unfolded, i.e. |
| 513 |
* lines that begin with whitespace are appended |
| 514 |
* to current line. E.g. |
| 515 |
* |
| 516 |
* Field-Name: Blaa-Blaa |
| 517 |
* This-Is-A-Continuation-Line |
| 518 |
* Here-Is_Another |
| 519 |
* |
| 520 |
* is seen by the caller as: |
| 521 |
* |
| 522 |
* Field-Name: Blaa-Blaa This-Is-A-Continuation-Line Here-Is_Another |
| 523 |
* |
| 524 |
*/ |
| 525 |
char *HTAA_getUnfoldedLine(void) |
| 526 |
{ |
| 527 |
char *line = NULL; |
| 528 |
char *cur; |
| 529 |
int count; |
| 530 |
BOOL peek_for_folding = NO; |
| 531 |
|
| 532 |
if (in_soc < 0) { |
| 533 |
CTRACE((tfp, "%s %s\n", |
| 534 |
"HTAA_getUnfoldedLine: buffer not initialized", |
| 535 |
"with function HTAA_setupReader()")); |
| 536 |
return NULL; |
| 537 |
} |
| 538 |
|
| 539 |
for (;;) { |
| 540 |
|
| 541 |
/* Reading from socket */ |
| 542 |
|
| 543 |
if (start_pointer >= end_pointer) { /*Read the next block and continue */ |
| 544 |
#ifdef USE_SSL |
| 545 |
if (SSL_handle) |
| 546 |
count = SSL_read(SSL_handle, buffer, BUFFER_SIZE); |
| 547 |
else |
| 548 |
count = NETREAD(in_soc, buffer, BUFFER_SIZE); |
| 549 |
#else |
| 550 |
count = NETREAD(in_soc, buffer, BUFFER_SIZE); |
| 551 |
#endif /* USE_SSL */ |
| 552 |
if (count <= 0) { |
| 553 |
in_soc = -1; |
| 554 |
return line; |
| 555 |
} |
| 556 |
if (count > (int) buffer_length) |
| 557 |
count = (int) buffer_length; |
| 558 |
start_pointer = buffer; |
| 559 |
end_pointer = buffer + count; |
| 560 |
*end_pointer = '\0'; |
| 561 |
#ifdef NOT_ASCII |
| 562 |
cur = start_pointer; |
| 563 |
while (cur < end_pointer) { |
| 564 |
*cur = TOASCII(*cur); |
| 565 |
cur++; |
| 566 |
} |
| 567 |
#endif /*NOT_ASCII */ |
| 568 |
} |
| 569 |
cur = start_pointer; |
| 570 |
|
| 571 |
/* Unfolding */ |
| 572 |
|
| 573 |
if (peek_for_folding) { |
| 574 |
if (*cur != ' ' && *cur != '\t') |
| 575 |
return line; /* Ok, no continuation line */ |
| 576 |
else /* So this is a continuation line, continue */ |
| 577 |
peek_for_folding = NO; |
| 578 |
} |
| 579 |
|
| 580 |
/* Finding end-of-line */ |
| 581 |
|
| 582 |
while (cur < end_pointer && *cur != '\n') /* Find the end-of-line */ |
| 583 |
cur++; /* (or end-of-buffer). */ |
| 584 |
|
| 585 |
/* Terminating line */ |
| 586 |
|
| 587 |
if (cur < end_pointer) { /* So *cur==LF, terminate line */ |
| 588 |
*cur = '\0'; /* Overwrite LF */ |
| 589 |
if (*(cur - 1) == '\r') |
| 590 |
*(cur - 1) = '\0'; /* Overwrite CR */ |
| 591 |
peek_for_folding = YES; /* Check for a continuation line */ |
| 592 |
} |
| 593 |
|
| 594 |
/* Copying the result */ |
| 595 |
|
| 596 |
if (line) |
| 597 |
StrAllocCat(line, start_pointer); /* Append */ |
| 598 |
else |
| 599 |
StrAllocCopy(line, start_pointer); /* A new line */ |
| 600 |
|
| 601 |
start_pointer = cur + 1; /* Skip the read line */ |
| 602 |
|
| 603 |
} /* forever */ |
| 604 |
} |