struct bb_http_header *bb_http_req_header(struct bb_session_request *bbsr, char *key, size_t keylen) { off_t i; for(i=1;i<=bbsr->header_pos;i++) { if (!bb_stricmp(key, keylen, bbsr->headers[i].key, bbsr->headers[i].keylen)) { return &bbsr->headers[i]; } } return NULL; }
struct bb_dealer *bb_get_dealer(struct bb_acceptor *acceptor, char *name, size_t len) { struct bb_virtualhost *vhost = acceptor->vhosts; while(vhost) { if (!bb_stricmp(name, len, vhost->name, vhost->len)) { return vhost->dealers; } vhost = vhost->next; } return NULL; }
int bb_set_dealer(struct bb_session *bbs, char *name, size_t len) { struct bb_acceptor *acceptor = bbs->connection->acceptor; struct bb_virtualhost *vhost = acceptor->vhosts; while(vhost) { if (!bb_stricmp(name, len, vhost->name, vhost->len)) { if (vhost->dealers) { bbs->dealer = vhost->dealers->dealer; bbs->vhost = vhost; return 0; } } vhost = vhost->next; } return -1; }
static int bb_session_headers_complete(http_parser *parser) { //printf("headers parsed\n"); struct bb_session_request *bbsr = (struct bb_session_request *) parser->data; off_t i; //printf("%s %.*s HTTP/%d.%d\n", http_method_str(parser->method), (int) bbsr->headers[0].keylen, bbsr->headers[0].key, parser->http_major, parser->http_minor); /* for(i=1;i<=bbsr->header_pos;i++) { printf("%.*s: %.*s\n", (int) bbsr->headers[i].keylen, bbsr->headers[i].key, (int)bbsr->headers[i].vallen, bbsr->headers[i].value); } */ // ok get the Host header struct bb_http_header *bbhh = bb_http_req_header(bbsr, "Host", 4); if (!bbhh) { return -1; } if (!bbsr->bbs->dealer) { bbsr->bbs->dealer = bb_get_dealer(bbsr->bbs->acceptor, bbhh->value, bbhh->vallen); if (!bbsr->bbs->dealer) { return -1; } } if (parser->upgrade) { struct bb_http_header *bbhh = bb_http_req_header(bbsr, "Upgrade", 7); if (bbhh) { if (!bb_stricmp("websocket", 9, bbhh->value, bbhh->vallen)) { bbsr->type = BLASTBEAT_TYPE_WEBSOCKET; bb_send_websocket_handshake(bbsr); goto msg; } } } if (!http_should_keep_alive(parser)) { //printf("NO KEEP ALIVE !!!\n"); bbsr->close = 1; } msg: // now encode headers in a uwsgi packet and send it as "headers" message if (bb_uwsgi(bbsr)) { return -1; } bb_zmq_send_msg(bbsr->bbs->dealer->identity, bbsr->bbs->dealer->len, (char *) &bbsr->bbs->uuid_part1, BB_UUID_LEN, "uwsgi", 5, bbsr->uwsgi_buf, bbsr->uwsgi_pos); return 0; }