subuser *checkrecv(char *pSock, ape_socket *client, acetables *g_ape, char *ip_client) { unsigned int op; unsigned int isget = 0; subuser *user = NULL; int local = (strcmp(ip_client, CONFIG_VAL(Server, ip_local, g_ape->srv)) == 0); clientget *cget = xmalloc(sizeof(*cget)); if (strlen(pSock) < 3 || (local && getqueryip(pSock, cget->ip_get) == 0)) { // get query IP (from htaccess) free(cget); shutdown(client->fd, 2); return NULL; } if (!local) { strncpy(cget->ip_get, ip_client, 16); // get real IP (from socket) } cget->client = client; gethost(pSock, cget->host); if (strncasecmp(pSock, "GET", 3) == 0) { if (!fixpacket(pSock, 0) || (cget->get = getfirstparam(pSock, (local ? '&' : '?'))) == NULL) { free(cget); shutdown(client->fd, 2); return NULL; } else { isget = 1; } } else if (strncasecmp(pSock, "POST", 4) == 0) { if ((cget->get = getpost(pSock)) == NULL) { free(cget); shutdown(client->fd, 2); return NULL; } } else { free(cget); shutdown(client->fd, 2); return NULL; } fixpacket(cget->get, 1); if (isget) { urldecode(cget->get); } op = checkcmd(cget, gettransport(pSock), &user, g_ape); switch (op) { case CONNECT_SHUTDOWN: shutdown(client->fd, 2); break; case CONNECT_KEEPALIVE: break; } free(cget); return user; }
subuser *checkrecv(ape_socket *co, acetables *g_ape) { unsigned int op; http_state *http = co->parser.data; subuser *user = NULL; clientget cget; if (http->host == NULL) { shutdown(co->fd, 2); return NULL; } if (gettransport(http->uri) == TRANSPORT_WEBSOCKET) { char *origin = get_header_line(http->hlines, "Origin"); websocket_state *websocket; if (origin == NULL) { shutdown(co->fd, 2); return NULL; } PACK_TCP(co->fd); sendbin(co->fd, CONST_STR_LEN(WEBSOCKET_HARDCODED_HEADERS), 0, g_ape); sendbin(co->fd, CONST_STR_LEN("WebSocket-Origin: "), 0, g_ape); sendbin(co->fd, origin, strlen(origin), 0, g_ape); sendbin(co->fd, CONST_STR_LEN("\r\nWebSocket-Location: ws://"), 0, g_ape); sendbin(co->fd, http->host, strlen(http->host), 0, g_ape); sendbin(co->fd, http->uri, strlen(http->uri), 0, g_ape); sendbin(co->fd, CONST_STR_LEN("\r\n\r\n"), 0, g_ape); FLUSH_TCP(co->fd); co->parser = parser_init_stream(co); websocket = co->parser.data; websocket->http = http; /* keep http data */ return NULL; } if (http->data == NULL) { sendbin(co->fd, HEADER_DEFAULT, HEADER_DEFAULT_LEN, 0, g_ape); sendbin(co->fd, CONST_STR_LEN(CONTENT_NOTFOUND), 0, g_ape); safe_shutdown(co->fd, g_ape); return NULL; } cget.client = co; cget.ip_get = co->ip_client; cget.get = http->data; cget.host = http->host; cget.hlines = http->hlines; op = checkcmd(&cget, gettransport(http->uri), &user, g_ape); switch (op) { case CONNECT_SHUTDOWN: safe_shutdown(co->fd, g_ape); break; case CONNECT_KEEPALIVE: break; } return user; }