85 } |
85 } |
86 |
86 |
87 return S("/"); |
87 return S("/"); |
88 } |
88 } |
89 |
89 |
|
90 NSAPISession* nsapisession_create(pool_handle_t *pool) { |
|
91 NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession)); |
|
92 if(!sn) { |
|
93 return NULL; |
|
94 } |
|
95 |
|
96 ZERO(sn, sizeof(NSAPISession)); |
|
97 |
|
98 sn->sn.pool = pool; |
|
99 sn->allocator = util_pool_allocator(pool); |
|
100 |
|
101 sn->sn.client = pblock_create_pool(sn->sn.pool, 8); |
|
102 if(!sn->sn.client) { |
|
103 pool_free(pool, sn); |
|
104 return NULL; |
|
105 } |
|
106 sn->sn.fill = 1; |
|
107 |
|
108 return sn; |
|
109 } |
|
110 |
|
111 int nsapisession_setconnection(NSAPISession *sn, Connection *conn, netbuf *inbuf, IOStream **io) { |
|
112 SessionHandler *sh = conn->session_handler; |
|
113 WSBool ssl; |
|
114 IOStream *sio = sh->create_iostream(sh, conn, sn->sn.pool, &ssl); |
|
115 if(!sio) { |
|
116 return 1; |
|
117 } |
|
118 *io = sio; |
|
119 IOStream *http = httpstream_new(sn->sn.pool, sio); |
|
120 if(!http) { |
|
121 return 1; |
|
122 } |
|
123 sn->connection = conn; |
|
124 sn->netbuf = inbuf; |
|
125 sn->sn.csd = http; |
|
126 sn->sn.ssl = ssl; |
|
127 return 0; |
|
128 } |
|
129 |
90 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) { |
130 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) { |
91 // handle nsapi request |
131 // handle nsapi request |
92 |
132 |
93 // create pool |
133 // create pool |
94 pool_handle_t *pool = pool_create(); |
134 pool_handle_t *pool = pool_create(); |
95 |
135 |
96 // create nsapi data structures |
136 // create nsapi data structures |
97 NSAPISession *sn = pool_malloc(pool, sizeof(NSAPISession)); |
137 NSAPISession *sn = nsapisession_create(pool); |
98 if(sn == NULL) { |
138 if(sn == NULL) { |
99 /* TODO: error */ |
139 /* TODO: error */ |
100 } |
140 } |
101 ZERO(sn, sizeof(NSAPISession)); |
141 |
102 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
142 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
103 if(rq == NULL) { |
143 if(rq == NULL) { |
104 /* TODO: error */ |
144 /* TODO: error */ |
105 } |
145 } |
106 ZERO(rq, sizeof(NSAPIRequest)); |
146 ZERO(rq, sizeof(NSAPIRequest)); |
107 rq->rq.req_start = request->req_start; |
147 rq->rq.req_start = request->req_start; |
108 rq->phase = NSAPIAuthTrans; |
148 rq->phase = NSAPIAuthTrans; |
109 |
149 |
110 // fill session structure |
150 // fill session structure |
111 sn->connection = request->connection; |
151 IOStream *io = NULL; |
112 sn->netbuf = request->netbuf; |
152 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { |
113 sn->sn.pool = pool; |
153 // TODO: error |
114 SessionHandler *sh = request->connection->session_handler; |
154 } |
115 WSBool ssl; |
|
116 IOStream *io = sh->create_iostream(sh, request->connection, pool, &ssl); |
|
117 sn->sn.csd = httpstream_new(pool, io); |
|
118 sn->sn.ssl = ssl; |
|
119 |
|
120 sn->sn.client = pblock_create_pool(sn->sn.pool, 8); |
|
121 sn->sn.next = NULL; |
|
122 sn->sn.fill = 1; |
|
123 sn->sn.subject = NULL; |
|
124 |
155 |
125 if(!ev) { |
156 if(!ev) { |
126 ev = ev_instance(get_default_event_handler()); |
157 ev = ev_instance(get_default_event_handler()); |
127 } |
158 } |
128 sn->sn.ev = ev; |
159 sn->sn.ev = ev; |
129 |
160 |
130 // the session needs the current server configuration |
161 // the session needs the current server configuration |
131 sn->config = request->connection->listener->cfg; |
162 sn->config = request->connection->listener->cfg; |
132 |
163 |
133 // add ip to sn->client pblock |
164 // add ip to sn->client pblock |
134 char ip_str[INET_ADDRSTRLEN]; |
165 char ip_str[INET_ADDRSTRLEN]; |