📄 htaautil.c
字号:
** ON ENTRY:** docname is the document pathname (from URL).**** ON EXIT:** returns a template matching docname, and other files** files in that directory.**** E.g. /foo/bar/x.html => /foo/bar/ *** ^** Space only to prevent it from** being a comment marker here,** there really isn't any space.*/PUBLIC char *HTAA_makeProtectionTemplate ARGS1(CONST char *, docname){ char *template = NULL; char *slash = NULL; if (docname) { StrAllocCopy(template, docname); slash = strrchr(template, '/'); if (slash) slash++; else slash = template; *slash = '\0'; StrAllocCat(template, "*"); } else StrAllocCopy(template, "*"); CTRACE((tfp, "make_template: made template `%s' for file `%s'\n", template, docname)); return template;}/*** Skip leading whitespace from *s forward*/#define SKIPWS(s) while (*s==' ' || *s=='\t') s++;/*** Kill trailing whitespace starting from *(s-1) backwards*/#define KILLWS(s) {char *c=s-1; while (*c==' ' || *c=='\t') *(c--)='\0';}/* PUBLIC HTAA_parseArgList()** PARSE AN ARGUMENT LIST GIVEN IN A HEADER FIELD** ON ENTRY:** str is a comma-separated list:**** item, item, item** where** item ::= value** | name=value** | name="value"**** Leading and trailing whitespace is ignored** everywhere except inside quotes, so the following** examples are equal:**** name=value,foo=bar** name="value",foo="bar"** name = value , foo = bar** name = "value" , foo = "bar"**** ON EXIT:** returns a list of name-value pairs (actually HTAssocList*).** For items with no name, just value, the name is** the number of order number of that item. E.g.** "1" for the first, etc.*/PUBLIC HTAssocList *HTAA_parseArgList ARGS1(char *, str){ HTAssocList *assoc_list = HTAssocList_new(); char *cur = NULL; char *name = NULL; int n = 0; if (!str) return assoc_list; while (*str) { SKIPWS(str); /* Skip leading whitespace */ cur = str; n++; while (*cur && *cur != '=' && *cur != ',') cur++; /* Find end of name (or lonely value without a name) */ KILLWS(cur); /* Kill trailing whitespace */ if (*cur == '=') { /* Name followed by a value */ *(cur++) = '\0'; /* Terminate name */ StrAllocCopy(name, str); SKIPWS(cur); /* Skip WS leading the value */ str = cur; if (*str == '"') { /* Quoted value */ str++; cur = str; while (*cur && *cur != '"') cur++; if (*cur == '"') *(cur++) = '\0'; /* Terminate value */ /* else it is lacking terminating quote */ SKIPWS(cur); /* Skip WS leading comma */ if (*cur == ',') cur++; /* Skip separating colon */ } else { /* Unquoted value */ while (*cur && *cur != ',') cur++; KILLWS(cur); /* Kill trailing whitespace */ if (*cur == ',') *(cur++) = '\0'; /* else *cur already NULL */ } } else { /* No name, just a value */ if (*cur == ',') *(cur++) = '\0'; /* Terminate value */ /* else last value on line (already terminated by NULL) */ HTSprintf0(&name, "%d", n); /* Item order number for name */ } HTAssocList_add(assoc_list, name, str); str = cur; } /* while *str */ FREE(name); return assoc_list;}/************** HEADER LINE READER -- DOES UNFOLDING *************************/#define BUFFER_SIZE 1024PRIVATE size_t buffer_length;PRIVATE char *buffer = 0;PRIVATE char *start_pointer;PRIVATE char *end_pointer;PRIVATE int in_soc = -1;#ifdef LY_FIND_LEAKSPRIVATE void FreeHTAAUtil NOARGS{ FREE(buffer);}#endif /* LY_FIND_LEAKS *//* PUBLIC HTAA_setupReader()** SET UP HEADER LINE READER, i.e., give** the already-read-but-not-yet-processed** buffer of text to be read before more** is read from the socket.** ON ENTRY:** start_of_headers is a pointer to a buffer containing** the beginning of the header lines** (rest will be read from a socket).** length is the number of valid characters in** 'start_of_headers' buffer.** soc is the socket to use when start_of_headers** buffer is used up.** ON EXIT:** returns nothing.** Subsequent calls to HTAA_getUnfoldedLine()** will use this buffer first and then** proceed to read from socket.*/PUBLIC void HTAA_setupReader ARGS3(char *, start_of_headers, int, length, int, soc){ if (!start_of_headers) length = 0; /* initialize length (is this reached at all?) */ if (buffer == NULL) { /* first call? */ buffer_length = length; if (buffer_length < BUFFER_SIZE) /* would fall below BUFFER_SIZE? */ buffer_length = BUFFER_SIZE; buffer = (char*)malloc((size_t)(sizeof(char)*(buffer_length + 1))); } else if (length > (int)buffer_length) { /* need more space? */ buffer_length = length; buffer = (char*)realloc((char*)buffer, (size_t)(sizeof(char)*(buffer_length + 1))); } if (buffer == NULL) outofmem(__FILE__, "HTAA_setupReader");#ifdef LY_FIND_LEAKS atexit(FreeHTAAUtil);#endif start_pointer = buffer; if (start_of_headers) { strncpy(buffer, start_of_headers, length); buffer[length] = '\0'; end_pointer = buffer + length; } else { *start_pointer = '\0'; end_pointer = start_pointer; } in_soc = soc;}/* PUBLIC HTAA_getUnfoldedLine()** READ AN UNFOLDED HEADER LINE FROM SOCKET** ON ENTRY:** HTAA_setupReader must absolutely be called before** this function to set up internal buffer.**** ON EXIT:** returns a newly-allocated character string representing** the read line. The line is unfolded, i.e.** lines that begin with whitespace are appended** to current line. E.g.**** Field-Name: Blaa-Blaa** This-Is-A-Continuation-Line** Here-Is_Another**** is seen by the caller as:**** Field-Name: Blaa-Blaa This-Is-A-Continuation-Line Here-Is_Another***/PUBLIC char *HTAA_getUnfoldedLine NOARGS{ char *line = NULL; char *cur; int count; BOOL peek_for_folding = NO; if (in_soc < 0) { CTRACE((tfp, "%s %s\n", "HTAA_getUnfoldedLine: buffer not initialized", "with function HTAA_setupReader()")); return NULL; } for(;;) { /* Reading from socket */ if (start_pointer >= end_pointer) {/*Read the next block and continue*/#ifdef USE_SSL if (SSL_handle) count = SSL_read(SSL_handle, buffer, BUFFER_SIZE); else count = NETREAD(in_soc, buffer, BUFFER_SIZE);#else count = NETREAD(in_soc, buffer, BUFFER_SIZE);#endif /* USE_SSL */ if (count <= 0) { in_soc = -1; return line; } start_pointer = buffer; end_pointer = buffer + count; *end_pointer = '\0';#ifdef NOT_ASCII cur = start_pointer; while (cur < end_pointer) { *cur = TOASCII(*cur); cur++; }#endif /*NOT_ASCII*/ } cur = start_pointer; /* Unfolding */ if (peek_for_folding) { if (*cur != ' ' && *cur != '\t') return line; /* Ok, no continuation line */ else /* So this is a continuation line, continue */ peek_for_folding = NO; } /* Finding end-of-line */ while (cur < end_pointer && *cur != '\n') /* Find the end-of-line */ cur++; /* (or end-of-buffer). */ /* Terminating line */ if (cur < end_pointer) { /* So *cur==LF, terminate line */ *cur = '\0'; /* Overwrite LF */ if (*(cur-1) == '\r') *(cur-1) = '\0'; /* Overwrite CR */ peek_for_folding = YES; /* Check for a continuation line */ } /* Copying the result */ if (line) StrAllocCat(line, start_pointer); /* Append */ else StrAllocCopy(line, start_pointer); /* A new line */ start_pointer = cur+1; /* Skip the read line */ } /* forever */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -