~ubuntu-branches/ubuntu/vivid/libhtp/vivid-proposed

« back to all changes in this revision

Viewing changes to htp/htp_util.c

  • Committer: Package Import Robot
  • Author(s): Pierre Chifflier
  • Date: 2012-11-03 09:23:06 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20121103092306-1mlez1c0xf0ic8t8
Tags: 0.2.10-1
* Imported Upstream version 0.2.10
* Update watch file
* Update symbols file
* Bump Standards Version
* Convert to DH 9
  - add multiarch support
  - fully enable hardening flags
* Debian release 0.2.10-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
147
147
 * @return Method number of M_UNKNOWN
148
148
 */
149
149
int htp_convert_method_to_number(bstr *method) {
 
150
    if (method == NULL) return M_UNKNOWN;
150
151
    // TODO Optimize using parallel matching, or something
151
152
    if (bstr_cmpc(method, "GET") == 0) return M_GET;
152
153
    if (bstr_cmpc(method, "PUT") == 0) return M_PUT;
306
307
    va_start(args, fmt);
307
308
 
308
309
    int r = vsnprintf(buf, 1023, fmt, args);
309
 
    
 
310
 
310
311
    va_end(args);
311
312
 
312
313
    if (r < 0) {
337
338
        connp->last_error = log;
338
339
    }
339
340
 
340
 
    hook_run_all(connp->cfg->hook_log, log);   
 
341
    hook_run_all(connp->cfg->hook_log, log);
341
342
}
342
343
 
343
344
/**
446
447
 * @return HTP_ERROR on memory allocation failure, HTP_OK otherwise
447
448
 */
448
449
int htp_parse_uri(bstr *input, htp_uri_t **uri) {
 
450
    if (input == NULL)
 
451
        return HTP_ERROR;
449
452
    char *data = bstr_ptr(input);
450
453
    size_t len = bstr_len(input);
451
454
    size_t start, pos;
920
923
 * @param path
921
924
 */
922
925
int htp_decode_path_inplace(htp_cfg_t *cfg, htp_tx_t *tx, bstr *path) {
 
926
    if (path == NULL)
 
927
        return -1;
 
928
 
923
929
    unsigned char *data = (unsigned char *) bstr_ptr(path);
924
930
    if (data == NULL) {
925
931
        return -1;
1228
1234
    if (incomplete->path != NULL) {
1229
1235
        // Make a copy of the path, on which we can work on
1230
1236
        normalized->path = bstr_strdup(incomplete->path);
1231
 
 
1232
 
        // Decode URL-encoded (and %u-encoded) characters, as well as lowercase,
1233
 
        // compress separators and convert backslashes.
1234
 
        htp_decode_path_inplace(connp->cfg, connp->in_tx, normalized->path);
1235
 
 
1236
 
        // Handle UTF-8 in path
1237
 
        if (connp->cfg->path_convert_utf8) {
1238
 
            // Decode Unicode characters into a single-byte stream, using best-fit mapping
1239
 
            htp_utf8_decode_path_inplace(connp->cfg, connp->in_tx, normalized->path);
1240
 
        } else {
1241
 
            // Only validate path as a UTF-8 stream
1242
 
            htp_utf8_validate_path(connp->in_tx, normalized->path);
 
1237
        if (normalized->path != NULL) {
 
1238
            // Decode URL-encoded (and %u-encoded) characters, as well as lowercase,
 
1239
            // compress separators and convert backslashes.
 
1240
            htp_decode_path_inplace(connp->cfg, connp->in_tx, normalized->path);
 
1241
 
 
1242
            // Handle UTF-8 in path
 
1243
            if (connp->cfg->path_convert_utf8) {
 
1244
                // Decode Unicode characters into a single-byte stream, using best-fit mapping
 
1245
                htp_utf8_decode_path_inplace(connp->cfg, connp->in_tx, normalized->path);
 
1246
            } else {
 
1247
                // Only validate path as a UTF-8 stream
 
1248
                htp_utf8_validate_path(connp->in_tx, normalized->path);
 
1249
            }
 
1250
 
 
1251
            // RFC normalization
 
1252
            htp_normalize_uri_path_inplace(normalized->path);
1243
1253
        }
1244
 
 
1245
 
        // RFC normalization
1246
 
        htp_normalize_uri_path_inplace(normalized->path);
1247
1254
    }
1248
1255
 
1249
1256
    // Query
1710
1717
            return "DONE";
1711
1718
    }
1712
1719
 
1713
 
    return "UNKOWN";
 
1720
    return "UNKNOWN";
1714
1721
}
1715
1722
 
1716
1723
bstr *htp_unparse_uri_noencode(htp_uri_t *uri) {
1830
1837
    for (i = 0; i < list_size(tx->request_header_lines); i++) {
1831
1838
        htp_header_line_t *hl = list_get(tx->request_header_lines, i);
1832
1839
        len += bstr_len(hl->line);
 
1840
        if (hl->terminators)
 
1841
            len += bstr_len(hl->terminators);
 
1842
        else
 
1843
            len += 2; // 0d 0a
1833
1844
    }
1834
1845
 
1835
1846
    request_headers_raw = bstr_alloc(len);
1841
1852
    for (i = 0; i < list_size(tx->request_header_lines); i++) {
1842
1853
        htp_header_line_t *hl = list_get(tx->request_header_lines, i);
1843
1854
        bstr_add_str_noex(request_headers_raw, hl->line);
 
1855
        if (hl->terminators)
 
1856
            bstr_add_str_noex(request_headers_raw, hl->terminators);
 
1857
        else
 
1858
            bstr_add_cstr_noex(request_headers_raw, "\r\n");
1844
1859
    }
1845
1860
 
1846
1861
    return request_headers_raw;
1878
1893
 
1879
1894
    return tx->request_headers_raw;
1880
1895
}
 
1896
 
 
1897
/**
 
1898
 * Construct a bstr that contains the raw response headers.
 
1899
 *
 
1900
 * @param tx
 
1901
 * @return
 
1902
 */
 
1903
bstr *htp_tx_generate_response_headers_raw(htp_tx_t *tx) {
 
1904
    bstr *response_headers_raw = NULL;
 
1905
    size_t i, len = 0;
 
1906
 
 
1907
    for (i = 0; i < list_size(tx->response_header_lines); i++) {
 
1908
        htp_header_line_t *hl = list_get(tx->response_header_lines, i);
 
1909
        len += bstr_len(hl->line);
 
1910
        if (hl->terminators)
 
1911
            len += bstr_len(hl->terminators);
 
1912
        else
 
1913
            len += 2; // 0d 0a
 
1914
    }
 
1915
 
 
1916
    response_headers_raw = bstr_alloc(len);
 
1917
    if (response_headers_raw == NULL) {
 
1918
        htp_log(tx->connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Failed to allocate bstring of %d bytes", len);
 
1919
        return NULL;
 
1920
    }
 
1921
 
 
1922
    for (i = 0; i < list_size(tx->response_header_lines); i++) {
 
1923
        htp_header_line_t *hl = list_get(tx->response_header_lines, i);
 
1924
        bstr_add_str_noex(response_headers_raw, hl->line);
 
1925
        if (hl->terminators)
 
1926
            bstr_add_str_noex(response_headers_raw, hl->terminators);
 
1927
        else
 
1928
            bstr_add_cstr_noex(response_headers_raw, "\r\n");
 
1929
    }
 
1930
 
 
1931
    return response_headers_raw;
 
1932
}
 
1933
 
 
1934
/**
 
1935
 * Get a bstr that contains the raw response headers. This method will always
 
1936
 * return an up-to-date buffer, containing the last known headers. Thus, if
 
1937
 * it is called once after RESPONSE_HEADERS phase it will return one buffer, but
 
1938
 * it may return a different buffer if called after RESPONSE_TRAILERS phase (but
 
1939
 * only if the response actually contains trailer headers). Do not retain the
 
1940
 * bstr pointer, as the buffer may change. If there are no changes to the
 
1941
 * response header structure, only one buffer will be contstructed and used. (Multiple
 
1942
 * invocations of this method will not cause multiple buffers to be created.)
 
1943
 *
 
1944
 * @param tx
 
1945
 * @return
 
1946
 */
 
1947
bstr *htp_tx_get_response_headers_raw(htp_tx_t *tx) {
 
1948
    // Check that we are not called too early
 
1949
    if (tx->progress < TX_PROGRESS_RES_HEADERS) return NULL;
 
1950
 
 
1951
    if (tx->response_headers_raw == NULL) {
 
1952
        tx->response_headers_raw = htp_tx_generate_response_headers_raw(tx);
 
1953
        tx->response_headers_raw_lines = list_size(tx->response_header_lines);
 
1954
    } else {
 
1955
        // Check that the buffer we have is not obsolete
 
1956
        if (tx->response_headers_raw_lines < list_size(tx->response_header_lines)) {
 
1957
            // Rebuild raw buffer
 
1958
            bstr_free(tx->response_headers_raw);
 
1959
            tx->response_headers_raw = htp_tx_generate_response_headers_raw(tx);
 
1960
            tx->response_headers_raw_lines = list_size(tx->response_header_lines);
 
1961
        }
 
1962
    }
 
1963
 
 
1964
    return tx->response_headers_raw;
 
1965
}