File: rewrite.c

package info (click to toggle)
mongrel2 1.12.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,020 kB
  • sloc: ansic: 39,099; python: 2,833; sql: 1,555; javascript: 1,202; sh: 467; makefile: 360; asm: 189; yacc: 145; php: 73; awk: 28; sed: 5
file content (66 lines) | stat: -rw-r--r-- 1,943 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <filter.h>
#include <dbg.h>
#include <tnetstrings.h>

static struct tagbstring rewritePath=bsStatic("/proxy/");
static struct tagbstring newPath=bsStatic("/");

StateEvent filter_transition(StateEvent state, Connection *conn, tns_value_t *config)
{

    log_info("REWRITE: %s", bdata(conn->req->path));
    if(0==bstrncmp(conn->req->path,&rewritePath,blength(&rewritePath))) {
        bstring header = bfromcstralloc(1024,"");
        hscan_t scan;
        hnode_t *n = NULL;
        Request *req = conn->req;
        bstring newpath=bstrcpy(conn->req->path);

        bconcat(header,req->request_method);
        bconchar(header,' ');
        breplace(newpath,0,blength(&rewritePath),&newPath,0);
        //bconcat(header,req->path);
        bconcat(header,newpath);
        bdestroy(newpath);
        newpath=NULL;
        bconchar(header,' ');
        bconcat(header,req->version);
        bcatcstr(header,"\r\n");

        hash_scan_begin(&scan,req->headers);
        for(n = hash_scan_next(&scan); n != NULL; n = hash_scan_next(&scan)) {
            struct bstrList *val_list = hnode_get(n);
            bstring key;
            int i;
            if(val_list->qty == 0) continue;
            key = (bstring)hnode_getkey(n);
            bconcat(header,key);
            bconchar(header,':');
            bconcat(header,val_list->entry[0]);
            for(i=1;i<val_list->qty;++i) {
                bconchar(header,',');
                bconcat(header,val_list->entry[i]);
            }
            bcatcstr(header,"\r\n");
        }
        bcatcstr(header,"\r\n");

        req->new_header=header;
    }

    return state;
}


StateEvent *filter_init(Server *srv, bstring load_path, int *out_nstates)
{
    StateEvent states[] = {PROXY};
    *out_nstates = Filter_states_length(states);
    check(*out_nstates == 1, "Wrong state array length.");

    return Filter_state_list(states, *out_nstates);

error:
    return NULL;
}