int main( int argc, char** argv ) { long long i, count; char* line = malloc( BUFFERSIZE ); if( argc > 1 ) { if( !strcmp( argv[1], "--help" ) ) { printf( "Parses the script input at STDIN\n" ); printf( "Usage:\n" ); printf( "\twsparsertest [runs]\n" ); printf( "If number of runs is more than one, no parser tree is output.\n" ); return EXIT_SUCCESS; } if( !strcmp( argv[1], "--version" ) ) { printf( "libwsparser %i.%i (%s)\n", WS_PARSER_VERSION_MAJOR, WS_PARSER_VERSION_MINOR, WS_PARSER_VERSION ); return EXIT_SUCCESS; } count = atoll( argv[1] ); } else { count = 1; } FILE* file = stdin; i = 0; while( ( line[i] = fgetc( file ) ) != EOF && i < BUFFERSIZE ) i++; line[i] = '\0'; for( i = 0; i < count; i++ ) { ws_parser_output out; ws_parser_tree* tree; out = ws_parse( line ); if( out.errno ) { printf( "Error, errno: %i, err arg: %i, err line: %i\n", out.errno, out.errarg, out.errline ); return EXIT_FAILURE; } tree = out.tree; if( count < 2 ) printNodesRec( tree, tree->root, 0 ); ws_parser_tree_free( tree ); } free( line ); return EXIT_SUCCESS; }
int ws_process(struct tcp_connection *con) { struct ws_req *req; struct ws_req *newreq; long size = 0; enum ws_close_code ret_code = WS_ERR_NONE; unsigned char bk; char *msg_buf; int msg_len; struct receive_info local_rcv; if (con->con_req) { req=(struct ws_req *)con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); init_ws_req(&ws_current_req, 0); req=&ws_current_req; } again: if (req->tcp.error == TCP_REQ_OK) { if (req->tcp.parsed >= req->tcp.pos) { if (ws_raw_read(con, &req->tcp) < 0) { LM_ERR("failed to read %d:%s\n", errno, strerror(errno)); goto error; } } ret_code = ws_parse(req); if (ret_code) goto error; /* eof check: * is EOF if eof on fd and r. not complete yet, * if r. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->tcp.complete==0)) { LM_DBG("EOF received\n"); goto done; } /* sanity mask checks */ if ((WS_TYPE(con) == WS_CLIENT && req->is_masked) || (WS_TYPE(con) == WS_SERVER && !req->is_masked)) { LM_DBG("malformed WS msg - %s %s\n", req->is_masked ? "masked" : "not masked", WS_TYPE(con) == WS_CLIENT ? "client" : "server"); ret_code = WS_ERR_BADDATA; goto error; } } if (req->tcp.complete) { /* update the timeout - we succesfully read the request */ tcp_conn_set_lifetime(con, ws_send_timeout); con->timeout=con->lifetime; /* if we are here everything is nice and ok*/ update_stat( pt[process_no].load, +1 ); /* rcv.bind_address should always be !=0 */ bind_address=con->rcv.bind_address; con->rcv.proto_reserved1=con->id; /* copy the id */ size=req->tcp.pos-req->tcp.parsed; switch (req->op) { case WS_OP_CLOSE: if (req->tcp.content_len) { /* for now we are only interested in the code, not the reason */ ret_code = WS_CLOSE_CODE(req); switch(ret_code) { case WS_ERR_NORMAL: LM_DBG("Normal WebSocket close\n"); break; case WS_ERR_CLIENT: LM_DBG("Client error close\n"); break; case WS_ERR_PROTO: LM_DBG("WebSocket protocol error\n"); break; case WS_ERR_BADDATA: LM_DBG("Data type not consistent\n"); break; case WS_ERR_POLICY: LM_DBG("Bad policy close\n"); break; case WS_ERR_TOO_BIG: LM_DBG("Packet too big close\n"); break; case WS_ERR_BADEXT: LM_DBG("Bad extension close\n"); break; case WS_ERR_UNEXPECT: LM_DBG("Unexpected condition close\n"); break; default: LM_DBG("Unknown WebSocket close: %d\n", ret_code); } } else { ret_code = WS_ERR_NORMAL; } /* respond to close */ WS_CODE(con) = ret_code; ws_send_close(con); WS_CODE(con) = WS_ERR_NOSEND; /* release the connextion */ con->state = S_CONN_EOF; goto done; case WS_OP_PING: if (ws_send_pong(con, req) < 0) LM_ERR("cannot send PONG msg\n"); break; case WS_OP_PONG: LM_DBG("Received WebSocket PONG\n"); break; case WS_OP_TEXT: case WS_OP_BIN: bk = *req->tcp.parsed; *req->tcp.parsed = 0; msg_buf = req->tcp.body; msg_len = req->tcp.parsed-req->tcp.body; local_rcv = con->rcv; if (!size) { /* did not read any more things - we can release * the connection */ LM_DBG("We're releasing the connection in state %d \n", con->state); if (req != &ws_current_req) { /* we have the buffer in the connection tied buff - * detach it , release the conn and free it afterwards */ con->con_req = NULL; } /* TODO - we could indicate to the TCP net layer to release * the connection -> other worker may read the next available * message on the pipe */ } else { LM_DBG("We still have things on the pipe - " "keeping connection \n"); } if (receive_msg(msg_buf, msg_len, &local_rcv) <0) LM_ERR("receive_msg failed \n"); *req->tcp.parsed = bk; break; default: LM_BUG("Can't handle %d\n", req->op); goto error; } update_stat( pt[process_no].load, -1 ); if (size) memmove(req->tcp.buf, req->tcp.parsed, size); #ifdef EXTRA_DEBUG LM_DBG("preparing for new request, kept %ld bytes\n", size); #endif init_ws_req(req, size); con->msg_attempts = 0; /* if we still have some unparsed bytes, try to parse them too*/ if (size) goto again; /* cleanup the existing request */ if (req != &ws_current_req) pkg_free(req); } else { /* request not complete - check the if the thresholds are exceeded */ con->msg_attempts++; if (con->msg_attempts == ws_max_msg_chunks) { LM_ERR("Made %u read attempts but message is not complete yet - " "closing connection \n",con->msg_attempts); goto error; } if (req == &ws_current_req) { /* let's duplicate this - most likely another conn will come in */ LM_DBG("We didn't manage to read a full request\n"); newreq = pkg_malloc(sizeof(struct ws_req)); if (newreq == NULL) { LM_ERR("No more mem for dynamic con request buffer\n"); goto error; } if (req->tcp.pos != req->tcp.buf) { /* we have read some bytes */ memcpy(newreq->tcp.buf,req->tcp.buf,req->tcp.pos-req->tcp.buf); newreq->tcp.pos = newreq->tcp.buf + (req->tcp.pos-req->tcp.buf); } else { newreq->tcp.pos = newreq->tcp.buf; } if (req->tcp.start != req->tcp.buf) newreq->tcp.start = newreq->tcp.buf +(req->tcp.start-req->tcp.buf); else newreq->tcp.start = newreq->tcp.buf; if (req->tcp.parsed != req->tcp.buf) newreq->tcp.parsed =newreq->tcp.buf+(req->tcp.parsed-req->tcp.buf); else newreq->tcp.parsed = newreq->tcp.buf; if (req->tcp.body != 0) { newreq->tcp.body = newreq->tcp.buf + (req->tcp.body-req->tcp.buf); } else newreq->tcp.body = 0; newreq->tcp.complete=req->tcp.complete; newreq->tcp.has_content_len=req->tcp.has_content_len; newreq->tcp.content_len=req->tcp.content_len; newreq->tcp.bytes_to_go=req->tcp.bytes_to_go; newreq->tcp.error = req->tcp.error; newreq->tcp.state = req->tcp.state; newreq->op = req->op; newreq->mask = req->mask; newreq->is_masked = req->is_masked; con->con_req = (struct tcp_req *)newreq; } } LM_DBG("ws_read end\n"); done: /* connection will be released */ return size; error: WS_CODE(con) = ret_code; if (WS_CODE(con) != WS_ERR_NONE) { ws_send_close(con); WS_CODE(con) = WS_ERR_NOSEND; } return -1; }