http.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 Aug 2008 21:30:32 +0300
changeset 41 540737bf6bac
parent 27 1e79b4cc8f1b
permissions -rw-r--r--
sending requests, and partial support for receiving -- incomplete, not tested
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include <sys/queue.h>
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include <string.h>
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <stdlib.h>
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
     4
#include <limits.h>
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
     5
#include <assert.h>
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <event2/event_struct.h>
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include "http.h"
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
#include "common.h"
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
int http_qarg_parse (struct evhttp_request *request, struct http_qarg *qarg_spec) {
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    // our return code
12
43297144f196 * rename files, render_file -> file_main, render_node -> node_main, mandelbrot -> render_mandelbrot
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    14
    int error = -1;
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    // get the uri from the request
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    const char *uri = evhttp_request_get_uri(request);
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    // the keyval list that we get from evhttp_parse_query
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    struct evkeyval *qarg;
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    struct evkeyvalq qargs;
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    // parse the uri into a list
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    evhttp_parse_query(uri, &qargs);
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    26
    // the http_qarg that we iterate with
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    27
    struct http_qarg *qarg_info;
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    28
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    // iterate over the list of given query arugments
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    TAILQ_FOREACH(qarg, &qargs, next) {
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        // match them against our http_qarg structs
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        for (qarg_info = qarg_spec; qarg_info->hqa_type != QARG_END; qarg_info++) {
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
            if (strcmp(qarg->key, qarg_info->hqa_key) == 0) {
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
                // found a match
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
                
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
                // extract a value from the qarg
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
                char *cptr = NULL;
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    39
                unsigned long long int uint_val;
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
                switch (qarg_info->hqa_type) {
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    42
                    case QARG_UINT16 :
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    43
                    case QARG_UINT32 :
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    44
                    case QARG_UINT64 :
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    45
                        uint_val = strtoull(qarg->value, &cptr, 10);
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    47
                        if (
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    48
                                (*qarg->value == '\0' || *cptr != '\0')
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    49
                            ||  (qarg_info->hqa_type == QARG_UINT16 && uint_val > SHRT_MAX)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    50
                            ||  (qarg_info->hqa_type == QARG_UINT32 && uint_val > UINT_MAX)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    51
                            ||  (qarg_info->hqa_type == QARG_UINT64 && uint_val > ULLONG_MAX)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    52
                        )
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    53
                            ERROR("invalid QARG_UINT: %s: %s -> %lld", qarg->key, qarg->value, uint_val);
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    54
                        
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    55
                        if (qarg_info->hqa_type == QARG_UINT16)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    56
                            *((unsigned short int *) qarg_info->hqa_addr) = (unsigned short int) uint_val;
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    57
                        else if (qarg_info->hqa_type == QARG_UINT32)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    58
                            *((unsigned int *) qarg_info->hqa_addr) = (unsigned int) uint_val;
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    59
                        else if (qarg_info->hqa_type == QARG_UINT64)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    60
                            *((unsigned long long int *) qarg_info->hqa_addr) = (unsigned long long int) uint_val;
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    61
                        else
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    62
                            assert(0);
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
                        break;
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
                    
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
                    case QARG_INVALID :
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
                        ERROR("QARG_INVALID: %s: %s", qarg->key, qarg->value);
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
                    default :
12
43297144f196 * rename files, render_file -> file_main, render_node -> node_main, mandelbrot -> render_mandelbrot
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    70
                        FATAL("invalid qarg_info->hqa_type: %d", qarg_info->hqa_type);
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
                }
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    72
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    73
                // clear the QARG_REQUIRED flag
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    74
                qarg_info->hqa_flags &= ~QARG_REQUIRED;
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
            }
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        }
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    }
27
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    78
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    79
    // missing qargs?
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    80
    for (qarg_info = qarg_spec; qarg_info->hqa_type != QARG_END; qarg_info++) {
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    81
        if (qarg_info->hqa_flags & QARG_REQUIRED)
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    82
            ERROR("missing value for required qarg: %s", qarg_info->hqa_key);
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    83
    }
1e79b4cc8f1b support for static files (.css, .html, .js), and tiles - serves up a full viewer at / now, but the JS code needs cleaning up
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    84
12
43297144f196 * rename files, render_file -> file_main, render_node -> node_main, mandelbrot -> render_mandelbrot
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    85
    // success
43297144f196 * rename files, render_file -> file_main, render_node -> node_main, mandelbrot -> render_mandelbrot
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    86
    error = 0;
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
error:    
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    // clean up the qargs (badly named functions :< )
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    evhttp_clear_headers(&qargs);
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    
12
43297144f196 * rename files, render_file -> file_main, render_node -> node_main, mandelbrot -> render_mandelbrot
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    92
    return error;
10
9daa832ab9c4 separate http query argument parsing into a new http module, and clean up unused headers from web_main.c
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
}