Пример #1
0
void
sniff_http_client(struct tcp_stream *ts, void **yoda)
{
	int i;

	switch (ts->nids_state) {

	case NIDS_JUST_EST:
		ts->server.collect = 1;

	case NIDS_DATA:
		if (ts->server.count_new != 0) {
			i = process_http_request(&ts->addr, ts->server.data,
						 ts->server.count -
						 ts->server.offset);
			nids_discard(ts, i);
		}
		break;

	default:
		if (ts->server.count != 0) {
			process_http_request(&ts->addr, ts->server.data,
					     ts->server.count -
					     ts->server.offset);
		}
		break;
	}
}
Пример #2
0
static void http_loop(word pos)
{
    const char *request   = (const char *)ether.buffer + pos;
    BufferFiller response = ether.tcpOffset();

    // Process the request and build the response.
    process_http_request(request, response);

    // Send the response to the client.
    ether.httpServerReply(response.position());
}
Пример #3
0
int main(int argc, char *argv[])
{
	int listen_sock, connect_sock;
	int options = 1;
	struct sockaddr_in local_addr;
	struct sockaddr_in client_addr;
	socklen_t client_addr_len = sizeof(client_addr);
	int res;

	listen_sock = socket(PF_INET, SOCK_STREAM, 0); 

	if (listen_sock == -1) {
		perror("Unable to create listening socket");
		return listen_sock;
	}

	res = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
	if (res == -1) {
		perror("Unable to set listening socket options");
		return res;
	}
	
	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(HTTP_PORT);
	local_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	//local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	res = bind(listen_sock, (struct sockaddr *) &local_addr, sizeof(local_addr));
	if (res == -1) {
		perror("Unable to bind to local address");
		return res;
	}

	res = listen(listen_sock, 5);
	if (res == -1) {
		perror("Unable to set socket backlog");
		return res;
	}

	while((connect_sock = accept(listen_sock, (struct sockaddr *) &client_addr, &client_addr_len)) != -1) {
		//TODO: ideally this would spawn a new thread
		process_http_request(connect_sock);
	}

	if (connect_sock == -1)
		perror("Unable to accept socket");

	return 0;
}
Пример #4
0
void worker()
{
    while (true) {

        int socketfd = task_queue().pop();

        char recv_buff[1024];
        std::vector<char> request_buff;
        std::vector<char> response_buff;

        while (true) {

            int read_cnt = recv(socketfd, recv_buff,
                    sizeof(recv_buff), MSG_NOSIGNAL);

            if (-1 == read_cnt)
                break;

            if (0 == read_cnt)
                break;

            request_buff.insert(request_buff.end(),
                    recv_buff, recv_buff + read_cnt);

            if (process_http_request(request_buff, response_buff)) {

                ssize_t total_cnt = 0;
                ssize_t sent_cnt = 0;

                while (total_cnt != response_buff.size()) {

                    sent_cnt = send(socketfd, &response_buff[0] + total_cnt,
                            response_buff.size() - total_cnt, MSG_NOSIGNAL);

                    if (sent_cnt != -1)
                        total_cnt += sent_cnt;
                }


                shutdown(socketfd, SHUT_RDWR);
            }
        }
    }
}
Пример #5
0
/* http server */
int
web_application_thread()
{
    int sock, new_sd;
    struct sockaddr_in address, remote;
    int size;

    /* create a TCP socket */
    if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#ifdef OS_IS_FREERTOS
    vTaskDelete(NULL);
#endif
        return -1;
    }

    /* bind to port 80 at any interface */
    address.sin_family = AF_INET;
    address.sin_port = htons(http_port);
    address.sin_addr.s_addr = INADDR_ANY;
    if (lwip_bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0) {
#ifdef OS_IS_FREERTOS
    vTaskDelete(NULL);
#endif
        return -1;
    }

    /* listen for incoming connections */
    lwip_listen(sock, 0);

    size = sizeof(remote);
    while (1) {
        new_sd = lwip_accept(sock, (struct sockaddr *)&remote, (socklen_t *)&size);
        /* spawn a separate handler for each request */
        process_http_request((void*)new_sd);
        //sys_thread_new("httpd", (void(*)(void*))process_http_request, (void*)new_sd,
        //                THREAD_STACKSIZE,
        //                DEFAULT_THREAD_PRIO);
    }
    return 0;
}