Example #1
0
void test_post_web_page() {
    const char url[] = "http://localhost:3000/sprinklers/1/sensors/1/create_reading";
    const char req[] = "{\"sensor_reading\":{\"sensor_value\":10,\"read_time\":1349015174}}";
    StringBuffer* request = string_buffer_create();
    StringBuffer* response = string_buffer_create();
    string_buffer_write(request, req, strlen(req));
    
    bool result = post_web_page(url, request, response);
    assert(result);
    
    assert(strcmp(response->memory, ACK_STRING)==0);
    
    string_buffer_delete(request);
    string_buffer_delete(response);
    
}
Example #2
0
void test_get_web_page() {
    char url[] = SENSORS_CONFIGURATION_URL;
    StringBuffer* response = string_buffer_create();
    bool result = get_web_page(url, response);
    assert(result);
    string_buffer_delete(response);
}
static void receive_advert(
    const int socket,
    struct sockaddr *sa,
    const socklen_t socklen_in,
    void * const addr_src,
    const size_t host_len,
    new_indexnode_event_t packet_received_cb,
    void *packet_received_ctxt
)
{
    char host[host_len];
    char buf[1024];
    char *port, *fs2protocol, *id;
    string_buffer_t *buffer = string_buffer_new();
    int recv_rc;
    socklen_t socklen;


    /* For UDP it is specified that recv*() will return the whole packet in one
     * go. It is not correct to keep calling recv*() to get more of the message;
     * this isn't a stream. If the message is too big for the buffer it's simply
     * truncated. Usually silently, but by passing in MSG_TRUNC one gets the
     * real length of the message back, even if it has to be truncated. This
     * allows us to assert that our buffer is big enough. We've had to take a
     * guess because advert packets are variable length, but it's an assertion
     * because 1024 really should be enough */

    errno = 0;
    recv_rc = recvfrom(socket, buf, sizeof(buf) - 1, MSG_TRUNC, sa, &socklen);
    assert(recv_rc <= (int)sizeof(buf) - 1);

    if (recv_rc >= 0)
    {
        assert(socklen == socklen_in);
        buf[recv_rc] = '\0';
        string_buffer_append(buffer, strdup(buf));

        if (!parse_advert_packet(string_buffer_peek(buffer), &port, &fs2protocol, &id) &&
            inet_ntop(AF_INET, addr_src, host, sizeof(host) - 1))
        {
            packet_received_cb(packet_received_ctxt, strdup(host), port, fs2protocol, id);
        }
    }
    else
    {
        trace_warn("failed to recvfrom() and indexnode advert packet: %s\n", strerror(errno));
    }


    string_buffer_delete(buffer);
}
Example #4
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;
}
Example #5
0
void free_request(dino_handle_t *dhandle) {
    if (NULL != dhandle) {
        if (dhandle->http.request.url) {
            memory_free(dhandle->http.request.url);
            dhandle->http.request.url = NULL;
        }

        string_buffer_delete(dhandle->http.response.buffer_handle, true);
        dhandle->http.response.buffer_handle = NULL;

        dino_strmap_delete(dhandle->http.request.params_map);
        dhandle->http.request.params_map = NULL;

        dino_strmap_delete(dhandle->http.response.params_map);
        dhandle->http.response.params_map = NULL;
    }
}
Example #6
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;
}