📄 sflhttp.c
字号:
code [ 2] = 0xB6; meta [ 2] = "Acirc";
code [ 3] = 0xB7; meta [ 3] = "Agrave";
code [ 4] = 0x8F; meta [ 4] = "Aring";
code [ 5] = 0xC7; meta [ 5] = "Atilde";
code [ 6] = 0x8E; meta [ 6] = "Auml";
code [ 7] = 0x80; meta [ 7] = "Ccedil";
code [ 8] = 0xD1; meta [ 8] = "ETH";
code [ 9] = 0x90; meta [ 9] = "Eacute";
code [ 10] = 0xD2; meta [ 10] = "Ecirc";
code [ 11] = 0xD4; meta [ 11] = "Egrave";
code [ 12] = 0xD3; meta [ 12] = "Euml";
code [ 13] = 0xD6; meta [ 13] = "Iacute";
code [ 14] = 0xD7; meta [ 14] = "Icirc";
code [ 15] = 0xDE; meta [ 15] = "Igrave";
code [ 16] = 0xD8; meta [ 16] = "Iuml";
code [ 17] = 0xA5; meta [ 17] = "Ntilde";
code [ 18] = 0xE0; meta [ 18] = "Oacute";
code [ 19] = 0xE2; meta [ 19] = "Ocirc";
code [ 20] = 0xE3; meta [ 20] = "Ograve";
code [ 21] = 0x9D; meta [ 21] = "Oslash";
code [ 22] = 0xE5; meta [ 22] = "Otilde";
code [ 23] = 0x99; meta [ 23] = "Ouml";
code [ 24] = 0xE6; meta [ 24] = "THORN";
code [ 25] = 0xE9; meta [ 25] = "Uacute";
code [ 26] = 0xEA; meta [ 26] = "Ucirc";
code [ 27] = 0xEB; meta [ 27] = "Ugrave";
code [ 28] = 0x9A; meta [ 28] = "Uuml";
code [ 29] = 0xED; meta [ 29] = "Yacute";
code [ 30] = 0xA0; meta [ 30] = "aacute";
code [ 31] = 0x83; meta [ 31] = "acirc";
code [ 32] = 0xEF; meta [ 32] = "acute";
code [ 33] = 0x91; meta [ 33] = "aelig";
code [ 34] = 0x85; meta [ 34] = "agrave";
code [ 35] = '&'; meta [ 35] = "amp";
code [ 36] = 0x86; meta [ 36] = "aring";
code [ 37] = 0xC6; meta [ 37] = "atilde";
code [ 38] = 0x84; meta [ 38] = "auml";
code [ 39] = 0xDD; meta [ 39] = "brvbar";
code [ 40] = 0x87; meta [ 40] = "ccedil";
code [ 41] = 0xB8; meta [ 41] = "cedil";
code [ 42] = 0xA2; meta [ 42] = "cent";
code [ 43] = 0xB8; meta [ 43] = "copy";
code [ 44] = 0xCF; meta [ 44] = "curren";
code [ 45] = 0xF8; meta [ 45] = "deg";
code [ 46] = 0xF6; meta [ 46] = "divide";
code [ 47] = 0x82; meta [ 47] = "eacute";
code [ 48] = 0x88; meta [ 48] = "ecirc";
code [ 49] = 0xCA; meta [ 49] = "egrave";
code [ 50] = 0xF0; meta [ 50] = "eth";
code [ 51] = 0x89; meta [ 51] = "euml";
code [ 52] = 0xAC; meta [ 52] = "frac12";
code [ 53] = 0xAB; meta [ 53] = "frac14";
code [ 54] = 0xF3; meta [ 54] = "frac34";
code [ 55] = '>'; meta [ 55] = "gt";
code [ 56] = 0xA1; meta [ 56] = "iacute";
code [ 57] = 0x8C; meta [ 57] = "icirc";
code [ 58] = 0xAD; meta [ 58] = "iexcl";
code [ 59] = 0x8D; meta [ 59] = "igrave";
code [ 60] = 0xA8; meta [ 60] = "iquest";
code [ 61] = 0x8B; meta [ 61] = "iuml";
code [ 62] = 0xAE; meta [ 62] = "laquo";
code [ 63] = 0x91; meta [ 63] = "lsquo";
code [ 64] = '<'; meta [ 64] = "lt";
code [ 65] = 0xEE; meta [ 65] = "macr";
code [ 66] = 0xB5; meta [ 66] = "micro";
code [ 67] = 0xFA; meta [ 67] = "middot";
code [ 68] = ' '; meta [ 68] = "nbsp";
code [ 69] = 0xAA; meta [ 69] = "not";
code [ 70] = 0xA4; meta [ 70] = "ntilde";
code [ 71] = 0xF3; meta [ 71] = "oacute";
code [ 72] = 0x93; meta [ 72] = "ocirc";
code [ 73] = 0x95; meta [ 73] = "ograve";
code [ 74] = 0xA6; meta [ 74] = "ordf";
code [ 75] = 0xA7; meta [ 75] = "ordm";
code [ 76] = 0x9B; meta [ 76] = "oslash";
code [ 77] = 0xA2; meta [ 77] = "otilde";
code [ 78] = 0x94; meta [ 78] = "ouml";
code [ 79] = 0xF4; meta [ 79] = "para";
code [ 80] = 0xF1; meta [ 80] = "plusmn";
code [ 81] = 0x9C; meta [ 81] = "pound";
code [ 82] = '"'; meta [ 82] = "quot";
code [ 83] = 0xAF; meta [ 83] = "raquo";
code [ 84] = 0xA9; meta [ 84] = "reg";
code [ 85] = 0x92; meta [ 85] = "rsquo";
code [ 86] = 0xF5; meta [ 86] = "sect";
code [ 87] = 0xB0; meta [ 87] = "shy";
code [ 88] = 0xFB; meta [ 88] = "sup1";
code [ 89] = 0xFD; meta [ 89] = "sup2";
code [ 90] = 0xFC; meta [ 90] = "sup3";
code [ 91] = 0xE1; meta [ 91] = "szlig";
code [ 92] = 0xFE; meta [ 92] = "thorn";
code [ 93] = 0x9E; meta [ 93] = "times";
code [ 94] = 0xA3; meta [ 94] = "uacute";
code [ 95] = 0x96; meta [ 95] = "ucirc";
code [ 96] = 0x97; meta [ 96] = "ugrave";
code [ 97] = 0xA8; meta [ 97] = "uml";
code [ 98] = 0x81; meta [ 98] = "uuml";
code [ 99] = 0xEC; meta [ 99] = "yacute";
code [100] = 0xA5; meta [100] = "yen";
code [101] = 0x98; meta [101] = "yuml";
#endif
}
if (*input == '#') /* Numeric translation */
{
input++;
num = 0;
if (*input == 'x') /* Hex */
{
input++;
num = decode_hex (&input, 0);
input++;
}
else
FOREVER
{
if ((*input >= '0') && (*input <= '9'))
num = (num * 10) + *input - '0';
else
break;
input++;
}
if (*input != ';')
num = 0;
return num;
}
else /* Lookup meta-character */
{
min = 0;
max = META_COUNT;
while (max > min)
{
char_index = (max + min) / 2;
cmp = strncmp (input, meta [char_index], length);
if (cmp == 0)
return code [char_index];
if (cmp > 0)
min = char_index + 1;
else
max = char_index;
}
return 0;
}
}
/* ---------------------------------------------------------------------[<]-
Function: cgi_parse_query_vars
Synopsis: Parses a CGI query string and loads the resulting variables
into an existing symbol table, optionally prefixing each name with a
string. Returns the number of variables loaded. The prefix can be
NULL or empty if not required.
---------------------------------------------------------------------[>]-*/
int
cgi_parse_query_vars (
SYMTAB *symtab,
const char *query,
const char *prefix)
{
char
*query_var, /* Query variable name */
**query_vars, /* Query as string table */
*equals; /* Equal sign in variable */
int
string_nbr, /* Index into string table */
variables = 0; /* Number of variables loaded */
ASSERT (symtab);
if ((query_vars = http_query2strt (query)) == NULL)
return (0); /* Not enough memory */
for (string_nbr = 0; query_vars [string_nbr]; string_nbr++)
{
equals = strchr (query_vars [string_nbr], '=');
if (equals)
{
*equals = '\0'; /* Cut into two strings */
if (prefix == NULL)
prefix = ""; /* Make safe for xstrcpy() */
query_var = xstrcpy (NULL, prefix, query_vars [string_nbr], NULL);
sym_assume_symbol (symtab, query_var, equals + 1);
mem_strfree (&query_var);
*equals = '='; /* Restore previous state */
variables++; /* Count this variable */
}
}
strtfree (query_vars);
return (variables);
}
/* ---------------------------------------------------------------------[<]-
Function: cgi_parse_file_vars
Synopsis: Parses a CGI query string stored in a file, and loads the
resulting variables into an existing symbol table, optionally
prefixing each name with a string. Returns the number of variables
loaded. The prefix can be NULL or empty if not required. The
file data is assumed to be escaped (see http_escape()); the data
should not contain line breaks, spaces, or other unescaped chars.
The file should already have been opened: a typical use for this
function is to parse the values supplied in stdin. The maximum size
for the file is CGI_QUERY_FILE_MAX characters.
---------------------------------------------------------------------[>]-*/
int
cgi_parse_file_vars (
SYMTAB *symtab,
FILE *file,
const char *prefix,
size_t size)
{
/* Maximum size of a stream of HTTP query data coming from a file */
# define CGI_QUERY_FILE_MAX 65535U
char
*query; /* Data loaded from file */
size_t
read_size; /* Amount of data read from file */
int
variables = 0; /* Number of variables loaded */
ASSERT (file);
ASSERT (symtab);
ASSERT (size <= CGI_QUERY_FILE_MAX);
if ((query = mem_alloc (size + 1)) == NULL)
return (0);
read_size = fread (query, 1, size, file);
query [read_size] = '\0';
variables = cgi_parse_query_vars (symtab, query, prefix);
mem_free (query);
return (variables);
}
/* ---------------------------------------------------------------------[<]-
Function: http_multipart_decode
Synopsis: Parses a multipart-encoded file (as received by a web server as
POST data) and returns a HTTP-encoded string containing the field data,
in the format: "name=value&name=value&name=value...". For each field that
refers to an uploaded file (INPUT field with type FILE), creates a
temporary file holding the data. The name of this temporary file is put
into a generated variable, whose name is built by using the local format
string (ex: '%s_tmp'). The actual uploaded file is stored in
a temporary file whose name is generated by the SFL get_tmp_file_name()
function. So, data for a file upload field called "doc" will be stored in
a temporary file called (eg) "temp1234.tmp", and a field "doc_tmp" will be
added, with the value "temp1234.tmp". The HTTP-encoded string is returned
as a DESCR block, which you can decode using http_query2strt(), passing the
descriptor data. You must free the descriptor using mem_free() when you're
finished with it.
---------------------------------------------------------------------[>]-*/
DESCR *
http_multipart_decode (const char *mime_file, const char *store_path,
const char *local_format)
{
FILE
*f_source,
*f_tmp = NULL;
char
*tmp_name = NULL,
*p_head,
*p_data,
*p_next,
*buffer;
int
offset,
read_size,
rest_read_size;
static char
separator [80 + 1];
static int
sep_size;
SYMTAB
*table,
*header_tab;
qbyte
tmp_index = 1;
DESCR
*descr = NULL;
ASSERT (local_format);
if (strstr (local_format, "%s") == NULL)
return (NULL);
buffer = mem_alloc (MULTI_BUFFER_SIZE + REST_BUFFER_SIZE + 1);
if (buffer == NULL)
return (NULL);
table = sym_create_table ();
if (table == NULL)
{
mem_free (buffer);
return (NULL);
}
header_tab = sym_create_table ();
if (header_tab == NULL)
{
mem_free (buffer);
sym_delete_table (table);
return (NULL);
}
f_source = fopen (mime_file, "rb");
if (f_source == NULL)
{
mem_free (buffer);
sym_delete_table (table);
sym_delete_table (header_tab);
return (NULL);
}
memset (separator, 0, sizeof (separator));
separator [0] = 0x0D;
separator [1] = 0x0A;
fgets (&separator [2], 78, f_source);
strconvch (&separator [2] , '\r', '\0');
strconvch (&separator [2] , '\n', '\0');
sep_size = strlen (separator);
read_size = fread (buffer, 1, MULTI_BUFFER_SIZE, f_source);
p_next = buffer;
while (read_size > 0)
{
sym_empty_table (header_tab);
p_head = p_next;
p_data = (char *) memfind ((byte *) p_head,
MULTI_BUFFER_SIZE + REST_BUFFER_SIZE - (p_head - buffer),
(byte *) "\r\n\r\n", 4, FALSE);
if (p_data)
{
*p_data = '\0';
p_data += 4;
}
if (p_head)
{
multipart_decode_header (p_head, header_tab);
if (sym_lookup_symbol (header_tab, "filename") != NULL)
{
if (f_tmp != NULL)
{
ASSERT (tmp_name != NULL);
fclose (f_tmp);
f_tmp = NULL;
if (get_file_size (tmp_name) == 0)
file_delete (tmp_name);
}
tmp_name = get_tmp_file_name (store_path, &tmp_index, "tmp");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -