📄 wsp_headers.c
字号:
if (!parm || !value) { warning(0, "Skipping parameters"); goto error; } octstr_append(decoded, octstr_imm("; ")); octstr_append(decoded, parm); if (octstr_len(value) > 0) { octstr_append_char(decoded, '='); octstr_append(decoded, value); } octstr_destroy(parm); octstr_destroy(value); return 0;error: parse_skip_to_limit(context); octstr_destroy(parm); octstr_destroy(value); parse_set_error(context); return -1;}void wsp_unpack_all_parameters(ParseContext *context, Octstr *decoded){ int ret = 0; while (ret >= 0 && !parse_error(context) && parse_octets_left(context) > 0) { ret = unpack_parameter(context, decoded); }}/* Unpack parameters in the format used by credentials and challenge, * which differs from the format used by all other HTTP headers. */static void unpack_broken_parameters(ParseContext *context, Octstr *decoded){ int ret = 0; int first = 1; long pos; while (ret >= 0 && !parse_error(context) && parse_octets_left(context) > 0) { pos = octstr_len(decoded); ret = unpack_parameter(context, decoded); if (ret >= 0) { if (first) { /* Zap ';' */ octstr_delete(decoded, pos, 1); first = 0; } else { /* Replace ';' with ',' */ octstr_set_char(decoded, pos, first ? ' ' : ','); } } }}static void unpack_optional_q_value(ParseContext *context, Octstr *decoded){ if (parse_octets_left(context) > 0) { Octstr *qval = unpack_q_value(context); if (qval) { octstr_append(decoded, octstr_imm("; q=")); octstr_append(decoded, qval); octstr_destroy(qval); } else warning(0, "Bad q-value"); }}/* Date-value is defined in 8.4.2.3. */Octstr *wsp_unpack_date_value(ParseContext *context){ unsigned long timeval; int length; length = parse_get_char(context); if (length > 30) { warning(0, "WSP headers: bad date-value."); return NULL; } timeval = unpack_multi_octet_integer(context, length); if (timeval < 0) { warning(0, "WSP headers: cannot unpack date-value."); return NULL; } return date_format_http(timeval);}/* Accept-general-form is defined in 8.4.2.7 */Octstr *wsp_unpack_accept_general_form(ParseContext *context){ Octstr *decoded = NULL; int ret; long val; /* The definition for Accept-general-form looks quite complicated, * but the "Q-token Q-value" part fits the normal expansion of * Parameter, so it simplifies to: * Value-length Media-range *(Parameter) * and we've already parsed Value-length. */ /* We use this function to parse content-general-form too, * because its definition of Media-type is identical to Media-range. */ ret = wsp_secondary_field_value(context, &val); if (parse_error(context) || ret == WSP_FIELD_VALUE_NONE) { warning(0, "bad media-range or media-type"); return NULL; } if (ret == WSP_FIELD_VALUE_ENCODED) { decoded = wsp_content_type_to_string(val); if (!decoded) { warning(0, "Unknown content type 0x%02lx.", val); return NULL; } } else if (ret == WSP_FIELD_VALUE_NUL_STRING) { decoded = parse_get_nul_string(context); if (!decoded) { warning(0, "Format error in content type"); return NULL; } } else { panic(0, "Unknown secondary field value type %d.", ret); } wsp_unpack_all_parameters(context, decoded); return decoded;}/* Accept-charset-general-form is defined in 8.4.2.8 */Octstr *wsp_unpack_accept_charset_general_form(ParseContext *context){ Octstr *decoded = NULL; int ret; long val; ret = wsp_secondary_field_value(context, &val); if (parse_error(context) || ret == WSP_FIELD_VALUE_NONE) { warning(0, "Bad accept-charset-general-form"); return NULL; } if (ret == WSP_FIELD_VALUE_ENCODED) { decoded = wsp_charset_to_string(val); if (!decoded) { warning(0, "Unknown character set %04lx.", val); return NULL; } } else if (ret == WSP_FIELD_VALUE_NUL_STRING) { decoded = parse_get_nul_string(context); if (!decoded) { warning(0, "Format error in accept-charset"); return NULL; } } else { panic(0, "Unknown secondary field value type %d.", ret); } unpack_optional_q_value(context, decoded); return decoded;}/* Accept-language-general-form is defined in 8.4.2.10 */static Octstr *unpack_accept_language_general_form(ParseContext *context){ Octstr *decoded = NULL; int ret; long val; ret = wsp_secondary_field_value(context, &val); if (parse_error(context) || ret == WSP_FIELD_VALUE_NONE) { warning(0, "Bad accept-language-general-form"); return NULL; } if (ret == WSP_FIELD_VALUE_ENCODED) { /* Any-language is handled by a special entry in the * language table. */ decoded = wsp_language_to_string(val); if (!decoded) { warning(0, "Unknown language %02lx.", val); return NULL; } } else if (ret == WSP_FIELD_VALUE_NUL_STRING) { decoded = parse_get_nul_string(context); if (!decoded) { warning(0, "Format error in accept-language"); return NULL; } } else { panic(0, "Unknown secondary field value type %d.", ret); } unpack_optional_q_value(context, decoded); return decoded;}/* Credentials is defined in 8.4.2.5 */static Octstr *unpack_credentials(ParseContext *context){ Octstr *decoded = NULL; int val; val = parse_peek_char(context); if (val == BASIC_AUTHENTICATION) { Octstr *userid, *password; parse_skip(context, 1); userid = parse_get_nul_string(context); password = parse_get_nul_string(context); if (parse_error(context)) { octstr_destroy(userid); octstr_destroy(password); } else { /* Create the user-pass cookie */ decoded = octstr_duplicate(userid); octstr_append_char(decoded, ':'); octstr_append(decoded, password); /* XXX Deal with cookies that overflow the 76-per-line * limit of base64. Either go through and zap all * CR LF sequences, or give the conversion function * a flag or something to leave them out. */ octstr_binary_to_base64(decoded); /* Zap the CR LF at the end */ octstr_delete(decoded, octstr_len(decoded) - 2, 2); octstr_insert_data(decoded, 0, "Basic ", 6); octstr_destroy(userid); octstr_destroy(password); } } else if (val >= 32 && val < 128) { /* Generic authentication scheme */ decoded = parse_get_nul_string(context); if (decoded) unpack_broken_parameters(context, decoded); } if (!decoded) warning(0, "Cannot parse credentials."); return decoded;}/* Credentials is defined in 8.4.2.5 * but as Proxy-Authentication is to be used by kannel, * a simplier to parse version is used here */static Octstr *proxy_unpack_credentials(ParseContext *context){ Octstr *decoded = NULL; int val; val = parse_peek_char(context); if (val == BASIC_AUTHENTICATION) { Octstr *userid, *password; parse_skip(context, 1); userid = parse_get_nul_string(context); password = parse_get_nul_string(context); if (parse_error(context)) { octstr_destroy(userid); octstr_destroy(password); } else { /* Create the user-pass cookie */ decoded = octstr_duplicate(userid); octstr_append_char(decoded, ':'); octstr_append(decoded, password); octstr_destroy(userid); octstr_destroy(password); } } else if (val >= 32 && val < 128) { /* Generic authentication scheme */ decoded = parse_get_nul_string(context); if (decoded) unpack_broken_parameters(context, decoded); } if (!decoded) warning(0, "Cannot parse credentials."); return decoded;}/* Challenge is defined in 8.4.2.5 */static Octstr *unpack_challenge(ParseContext *context){ Octstr *decoded = NULL; Octstr *realm_value = NULL; int val; val = parse_peek_char(context); if (val == BASIC_AUTHENTICATION) { parse_skip(context, 1); realm_value = parse_get_nul_string(context); if (realm_value) { decoded = octstr_create("Basic realm=\""); octstr_append(decoded, realm_value); octstr_append_char(decoded, '"'); } } else if (val >= 32 && val < 128) { /* Generic authentication scheme */ decoded = parse_get_nul_string(context); realm_value = parse_get_nul_string(context); if (decoded && realm_value) { octstr_append(decoded, octstr_imm(" realm=\"")); octstr_append(decoded, realm_value); octstr_append_char(decoded, '"'); if (parse_octets_left(context) > 0) { /* Prepare for following parameter list */ octstr_append_char(decoded, ','); } unpack_broken_parameters(context, decoded); } } if (!decoded) warning(0, "Cannot parse challenge."); octstr_destroy(realm_value); return decoded;}/* Content-range is defined in 8.4.2.23 */static Octstr *unpack_content_range(ParseContext *context){ /* We'd have to figure out how to access the content range * length (i.e. user_data size) from here to parse this, * and I don't see why the _client_ would send this in any case. */ warning(0, "Decoding of content-range not supported"); return NULL; /* Octstr *decoded = NULL; unsigned long first_byte_pos, entity_length; unsigned long last_byte_pos; first_byte_pos = parse_get_uintvar(context); entity_length = parse_get_uintvar(context); if (parse_error(context)) { warning(0, "Cannot parse content-range header"); return NULL; } decoded = octstr_create("bytes "); octstr_append_decimal(decoded, first_byte_pos); octstr_append_char(decoded, '-'); octstr_append_decimal(decoded, last_byte_pos); octstr_append_char(decoded, '/'); octstr_append_decimal(decoded, entity_length); return decoded; */}/* Field-name is defined in 8.4.2.6 */static Octstr *unpack_field_name(ParseContext *context){ Octstr *decoded = NULL; int ret; int val; ret = wsp_field_value(context, &val); if (parse_error(context) || ret == WSP_FIELD_VALUE_DATA) { warning(0, "Bad field-name encoding"); return NULL; } if (ret == WSP_FIELD_VALUE_ENCODED) { decoded = wsp_header_to_string(val); if (!decoded) { warning(0, "Unknown field-name 0x%02x.", val); return NULL; } } else if (ret == WSP_FIELD_VALUE_NUL_STRING) { decoded = parse_get_nul_string(context); if (!decoded) { warning(0, "Bad field-name encoding"); return NULL; } } else { panic(0, "Unknown field value type %d.", ret); } return decoded;}/* Cache-directive is defined in 8.4.2.15 */static Octstr *unpack_cache_directive(ParseContext *context){ Octstr *decoded = NULL; int ret; int val; ret = wsp_field_value(context, &val); if (parse_error(context) || ret == WSP_FIELD_VALUE_DATA) { warning(0, "Bad cache-directive"); goto error; } if (ret == WSP_FIELD_VALUE_ENCODED) { decoded = wsp_cache_control_to_string(val); if (!decoded) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -