Пример #1
0
void DINO_EXPORT response_header_set(DHANDLE dhandle, const char *key, const char *value) {
    dino_http_response_t *response = cast_dhandle_response(dhandle);

    if (NULL != response) {
        if (key && *key) {
            dino_strmap_add(response->params_map, key, value);
        } else {
            log_message(LOG_ERR, __FUNCTION__, __FILE__, __LINE__, "Invalid key, must have at least one character...");
        }
    } else {
        log_message(LOG_ERR, __FUNCTION__, __FILE__, __LINE__, "Response is NULL...");
    }
}
Пример #2
0
size_t parse_headers(dino_http_data_t *http) {
    size_t content_size = 0;

    // Read the headers...
    //
    STRING_BUFFER_PTR string_buffer_ptr = NULL;

    while (read_line(http->socket, &string_buffer_ptr)) {
        // Find the key and the value
        //
        char *key = string_buffer_c_string(string_buffer_ptr);
        char *value = string_buffer_c_string(string_buffer_ptr);

        // Look for the ':'
        //
        for (; *value && *value != ':'; value++) { };

        // Get the "Value
        //
        if (*value == ':') {
            *value = 0;
            value++;
        }

        key = clean_string(key);
        value = clean_string(value);

        if (!dino_strmap_add(http->request.params_map, key, value)) {
            fprintf(stderr, "[WARNING] Unable to add to params list...\n\r");
        }

        // Check to see if we have hit the end...
        //
        if (key && *key == '\0') {
            // We hit the end!
            //
            break;
        }

        // OK look of content length
        //
        if (0 == strncmp(key, "Content-Length", 32)) {
            content_size = (size_t) atol(value);
        }
    }

    string_buffer_delete(string_buffer_ptr, true);
    return content_size;
}
Пример #3
0
bool bind_url_params(dino_http_request_t *request, dino_route_t *route, stack_char_ptr_t *url_stack) {
    // Now comapre the elements.
    //
    for (int index = 0; index < route->stack->count && index < url_stack->count; index++) {
        // If it is a wild card then Just skip it.
        //
        if (route->stack->pointers[index][0] == '*') {
            continue;
        }

        // Do we have a :param element?
        //
        if (route->stack->pointers[index][0] == ':') {
            dino_strmap_add(request->params_map, route->stack->pointers[index], url_stack->pointers[index]);
            continue;
        }
    }

    return true;
}
Пример #4
0
dino_http_method parse_method_url(dino_http_data_t *http) {
    // Should not happen:
    //
    if (NULL == http) {
        return http_invalid;
    }

    // Read in the data:
    //
    STRING_BUFFER_PTR string_buffer_ptr = NULL;

    if (0 == read_line(http->socket, &string_buffer_ptr)) {
        string_buffer_delete(string_buffer_ptr, true);
        return http_invalid;
    }

    // Obtain the method
    //
    char method[32];
    memory_clear(method, sizeof(method));
    int offset = 0;

    const char *line = string_buffer_c_string(string_buffer_ptr);

    for (int i = 0; !isspace(line[i]) && (i < sizeof(method)); i++) {
        method[i] = line[i];
        offset++;
    }

    http->request.method = map_string_to_http_method(method);

    if (http_invalid == http->request.method) {
        string_buffer_delete(string_buffer_ptr, true);
        return http_invalid;
    }

    // Fetch the URL
    //
    const char *query = line + offset;
    STRING_BUFFER_PTR buffer_url = string_buffer_new();

    // Skip over the ' 's and the tabs
    //
    while (*query != '\0' && (*query == ' ' || *query == '\t')) {
        ++query;
    }

    while (*query != '\0' && *query != '?' && *query != ' ' && *query != '\t') {
        string_buffer_append_char(buffer_url, *query);
        query++;
    }

    http->request.url = buffer_url;

    if (*query == '?') {
        // Move off of '?'
        //
        query++;

        offset = 0;

        STRING_BUFFER_PTR buffer_params = string_buffer_new();

        while (!isspace(*query) && *query != 0) {
            string_buffer_append_char(buffer_params, *query);
            offset++;
            query++;
        }

        // Parse the URL parameters
        //
        char *break_token = NULL;

        for (char *param = strtok_r(string_buffer_c_string(buffer_params), "&", &break_token);
             param;
             param = strtok_r(NULL, "&", &break_token))
        {
            char *break_token_2 = NULL;
            char *key = strtok_r(param, "=", &break_token_2);
            char *value = strtok_r(NULL, "=", &break_token_2);

            dino_strmap_add(http->request.params_map, key, value);
        }

        string_buffer_delete(buffer_params, true);
    }

    string_buffer_delete(string_buffer_ptr, true);
    return http->request.method;
}