コード例 #1
0
ファイル: weather.c プロジェクト: krautchan/boskop-ng
int reply(info_t * in) {
   int i;
   http_req_t *req;
   char buf[4096], *p;
   unsigned long now;

   if (in->cmd == cmd_privmsg) {
      in->tail = skip_nick(in->tail, in->me);
      if (!tail_cmd(&in->tail, "weather")) {
         now = time(NULL);
         if (!CHECKAUTH(in->sender, UL_OP) && lastcall > now - 10) {
            irc_privmsg(to_sender(in), "Please wait a moment");
            return 0;
         }
         
         if (!in->tail || !in->tail[0])
            return 0;

         p = config_get("weather.so", "url");
         if (!p || (i = strlen(p)) >= sizeof(buf))
            return 0;
         strcpy(buf, p);
         if (http_escape_str(in->tail, buf + i, sizeof(buf) -i))
            return 0;
         req = http_new(http_cb);
         if (!req)
            return 0;
         http_setptr(req, strdup(to_sender(in)));
         http_get(req, buf);
         lastcall = now;
      }
   }
   return 0;
}
コード例 #2
0
ファイル: server.c プロジェクト: Armen-Arakelian/Armen
void server_listen(server_t self){
    char input[8000];

    http_t httpRequest = http_new();
    while(1) {
        socket_t * clientSock = socket_accept(self->serverSock);
        socket_read(clientSock, input, sizeof(input));

        http_parse(httpRequest, input);

        _reply(self, httpRequest, clientSock);

        socket_free(clientSock);
    }
}
コード例 #3
0
ファイル: worker.c プロジェクト: dharmarth/webroar
void load_application(wkr_t* w){
  LOG_FUNCTION
  
  w->ctl->scgi = scgi_new();
  if(w->ctl->scgi == NULL) {
    LOG_ERROR(SEVERE,"Cannot create SCGI Request");
    sigproc();
    return;
  }
  
  w->http = http_new(w);
  if(w->http == NULL) {
    scgi_body_add(w->ctl->scgi, "unable to load application.", strlen("unable to load application."));
    LOG_ERROR(SEVERE,"unable to load application.");
  }else if(worker_listen(w) < 0) {
    scgi_body_add(w->ctl->scgi, "Error Initializing Workers.", strlen("Error Initializing Workers."));
    LOG_ERROR(WARN,"Error Initializing Workers.");
  }else{
    worker_accept_requests(w);
    LOG_INFO("Worker ready for serving requests.");
    init_idle_watcher(w);
    
    LOG_INFO("Successfully loaded rack application=%s with environment=%s",
             w->tmp->path.str,   w->tmp->env.str);
  }  
  
  //loading adapter according to application type
  LOG_DEBUG(DEBUG,"webroar_root = %s", w->tmp->root_path.str);
  LOG_DEBUG(DEBUG,"path = %s, name = %s, type = %s, environment = %s, baseuri = %s, analytics = %c",
            w->tmp->path.str,  w->tmp->name.str, w->tmp->type.str,
            w->tmp->env.str, w->tmp->resolver.str, w->tmp->profiler);
  
  // Send error or ok acknowledgement message
  if(w->ctl->scgi->body_length > 0){
    // Send error response
    w->ctl->error = TRUE;
    get_worker_add_ctl_scgi(w, TRUE);
    ev_io_start(w->loop,&(w->ctl->w_write));
    ev_timer_again(w->loop, &w->ctl->t_ack);
  }else{
    // Send acknowledgement message
    get_worker_add_ctl_scgi(w, FALSE);
    ev_io_start(w->loop,&(w->ctl->w_write));        
  }
  wkr_tmp_free(&w->tmp);
}
コード例 #4
0
ファイル: mini_tor.c プロジェクト: Luci4r/recycle_bin
int do_loop(void) {
    int efd;
    int sfd;
    int rc;
    int connfd;
    struct epoll_event event;
    struct epoll_event *events;

    tor_array_t *cache_sock;
    cache_sock = tor_array_new(TOR_MAX_EVENT, sizeof(http_context *));

    events = calloc(TOR_MAX_EVENT, sizeof(struct epoll_event));
    if (!events) {
        perror("");
        exit(EXIT_FAILURE);
    }


    rc = 0;
    sfd = create_and_bind(INADDR_ANY, config.port);
    if (sfd < 0) {
        exit(EXIT_FAILURE);
    }

    rc = set_fl(sfd, O_NONBLOCK);
    if (rc < 0) {
        exit(EXIT_FAILURE);
    }

    rc = listen(sfd, 10);
    if (rc < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    efd = epoll_create1(0);
    if (efd < 0) {
        perror("");
        exit(EXIT_FAILURE);
    }

    if (fd_add(efd, sfd) < 0)
        exit(EXIT_FAILURE);

    int i, n;
    http_context *http;
    for (;;) {
        n = epoll_wait(efd, events, TOR_MAX_EVENT, -1);
        for (i = 0; i < n; i++) {

            if ((events[i].events & EPOLLERR) || 
                    (events[i].events & EPOLLHUP) ||
                    !(events[i].events & EPOLLIN)) {
                close(events[i].data.fd);
                tor_log_err("epoll error %d\n", events[i].data.fd);

            } else if (events[i].data.fd == sfd) {

                for (;;) {

                    connfd = accept(sfd, NULL, NULL);

                    if (connfd == -1) {
                        if ((errno != EAGAIN) &&
                                (errno != EWOULDBLOCK)) {
                            perror("accept");
                        }
                        break;
                    }

                    if (set_fl(connfd, O_NONBLOCK) < 0) {
                        perror("set_fl");
                        exit(EXIT_FAILURE);
                    }

                    if (fd_add(efd, connfd) < 0) {
                        perror("fd_add");
                        exit(EXIT_FAILURE);
                    }

                    http = http_new(connfd, list_dir, sock_read, writen_nonblock);
                    
                    fprintf(stderr, "put fd = %d:sfd = %d http = %p\n", connfd, sfd, http);
                    tor_array_put(cache_sock, connfd, &http);
                }

            } else {
                fprintf(stderr, "get fd = %d\n", events[i].data.fd);
                http = *(http_context **)tor_array_get(cache_sock, events[i].data.fd);
                fprintf(stderr, "fd = %d\n", http->fd);
                http_read_head(http);
                do_process(http);
                http_free(http);
            }

        }

        fprintf(stderr, "=============\n");
    }

    close(sfd);
    close(efd);
}
コード例 #5
0
ファイル: flow.c プロジェクト: di3online/http-sniffer
/* Extract http_pair_t objects from flow's packet_t chain */
int 
flow_extract_http(flow_t *f)
{
	/* check if the flow is carrying HTTP again */
	if( f->http == FALSE)
		return 1;
		
	/*
	 * Find the actual FIN sequences.
	 */
	seq_t	*seq = f->order->src;
	seq_t	*src_fin_seq = NULL;
	seq_t	*dst_fin_seq = NULL;
	int found = 0;
	
	while(seq != NULL)
	{
		/* Update flow's first byte time.
		 * FBT of flow refers to the payload's FBT.
		 */
		if(seq->pkt != NULL && found == 0)
		{
			found = 1;
			f->fb_sec = seq->cap_sec;
			f->fb_usec = seq->cap_usec;
		}
		
		/*Search the FIN sequence in sequence queue.*/
		if(seq->th_flags & TH_FIN == TH_FIN)
		{
			src_fin_seq = seq;
			break;
		}
		seq = seq->next;
	}
	
	seq = f->order->dst;
	while(seq != NULL)
	{
		/*Search the FIN sequence in sequence queue.*/
		if(seq->th_flags & TH_FIN == TH_FIN)
		{
			dst_fin_seq = seq;
			break;
		}
		seq = seq->next;
	}
	
	/*
	 * Set the client and server FIN sequences.
	 */
	seq_t	*fin_seq = NULL;	/* The actual FIN sequence. */
	u_int8_t	fin_dir = 0;	/* fin_dir:
	 * 0: Not set;
	 * 1: src_fin_seq is used;
	 * 2: dst_fin_seq is used;
	 */
	
	if (src_fin_seq != NULL && dst_fin_seq == NULL)
	{
		fin_seq = src_fin_seq;
		fin_dir = 1;
	}
	else if (src_fin_seq == NULL && dst_fin_seq != NULL)
	{
		fin_seq = dst_fin_seq;
		fin_dir = 2;
	}
	else if (src_fin_seq != NULL && dst_fin_seq != NULL)
	{
		fin_seq = src_fin_seq;
		fin_dir = 1;
		if(compare_sequence_time(src_fin_seq, dst_fin_seq) == 1)
		{
			fin_seq = dst_fin_seq;
			fin_dir = 2;
		}
	}
	else
	{
		fin_seq = NULL;
		fin_dir = 0;
	}
	
	/* 
	 * First step: find requests 
	 */
	packet_t *pkt;
	request_t	*req;
	response_t	*rsp;
	int reqn = 0;	// Number of requests.
	int rspn = 0;	// Number of responses.
	
	http_pair_t *new_http = NULL;
	seq_t *seq_next = NULL;	/* for temp */
	seq_t *first_seq = NULL;
	/* Set seq and seq_next */
	seq = f->order->src;
	if( seq != NULL)
	{
		seq_next = seq->next;
	}
	else
	{
		seq_next = NULL;		/* NULL */
	}
	
	if (fin_seq != NULL && seq != NULL)
	{
		/*A FIN packet exists.*/
		while(compare_sequence_time(seq, fin_seq) < 0)
		{
			pkt = seq->pkt;
			if(pkt != NULL && pkt->http == HTTP_REQ)
			{
				/* When a new HTTP request is found,
				 * create a HTTP pair object, then add the object to
				 * flow's HTTP chain.
				 */
				reqn++;
				/* new HTTP pair object*/
				new_http = http_new();
				first_seq = seq;
				new_http->req_fb_sec = seq->cap_sec;
				new_http->req_fb_usec = seq->cap_usec;
				new_http->req_lb_sec = seq->cap_sec;
				new_http->req_lb_usec = seq->cap_usec;
					
				/* Add the object to flow's HTTP chain */
				flow_add_http(f, new_http);
				/* new request object */
				req = http_request_new();
				/* Add the request object to the foregoing HTTP pair object */
				http_add_request(new_http, req);
				/* parse and write values to foregoing request object */
				http_parse_request(req, pkt->tcp_odata, pkt->tcp_odata + pkt->tcp_dl);
			}
			else
			{
				/*Omit the TCP handshake sequences.*/
				if(new_http == NULL)
				{
					seq = seq->next;
					if(seq != NULL)
						seq_next = seq->next;
					else
						break;
					continue;
				}
			}

			if( new_http != NULL )
			{
				if( seq_next == NULL || seq_next == fin_seq || seq_next->pkt != NULL ||\
						compare_sequence_time(seq_next, fin_seq) >= 0 ){
					//assert(seq->nxt_seq != 0);
					if( seq->nxt_seq != 0){
						new_http->req_total_len = seq->nxt_seq - first_seq->seq;
						new_http->req_body_len = 0;
					}
					/*Update flow's last byte time.*/
					if ((seq->cap_sec > f->lb_sec) || (seq->cap_sec == f->lb_sec && seq->cap_usec > f->lb_usec))
					{
						f->lb_sec = seq->cap_sec;
						f->lb_usec = seq->cap_usec;
					}
				}
				else
				{
					//assert(seq->seq <= seq_next->seq);
				}
			}
			
			/* Continue to next sequence.*/
			seq = seq->next;
			if(seq != NULL)
				seq_next = seq->next;
			else
				break;
		}
	}
	else
	{
		/* No FIN packet found.*/
		while(seq != NULL)
		{
			pkt = seq->pkt;
			if(pkt != NULL && pkt->http == HTTP_REQ)
			{
				/* When a new HTTP request is found,
				 * create a HTTP pair object, then add the object to
				 * flow's HTTP chain.
				 */
				reqn++;
				/* new HTTP pair object*/
				new_http = http_new();
				first_seq = seq;
				new_http->req_fb_sec = seq->cap_sec;
				new_http->req_fb_usec = seq->cap_usec;
				new_http->req_lb_sec = seq->cap_sec;
				new_http->req_lb_usec = seq->cap_usec;
				
				/* Add the object to flow's HTTP chain */
				flow_add_http(f, new_http);
				/* new request object */
				req = http_request_new();
				/* Add the request object to the foregoing HTTP pair object */
				http_add_request(new_http, req);
				/* parse and write values to foregoing request object */
				http_parse_request(req, pkt->tcp_odata, pkt->tcp_odata + pkt->tcp_dl);
			}
			else
			{
				if(new_http == NULL)
				{
					/*Omit the TCP handshake sequences.*/
					seq = seq->next;
					if(seq != NULL)
						seq_next = seq->next;
					else
						break;
					continue;
				}
			}
			if( new_http != NULL )
			{
				if( seq_next == NULL || seq_next->pkt != NULL )
				{
					//assert(seq->nxt_seq != 0);
					if( seq->nxt_seq != 0){
						new_http->req_total_len = seq->nxt_seq - first_seq->seq;
						new_http->req_body_len = 0;
					}
					/*Update flow's last byte time.*/
					if ((seq->cap_sec > f->lb_sec) || (seq->cap_sec == f->lb_sec && seq->cap_usec > f->lb_usec))
					{
						f->lb_sec = seq->cap_sec;
						f->lb_usec = seq->cap_usec;
					}
				}
				else
				{
					//assert(seq->seq <= seq_next->seq);
				}
			}
			/*Continue to next sequence.*/
			seq = seq->next;
			if(seq != NULL)
				seq_next = seq->next;
			else
				break;
		}
	}

	/* If no responses found, we treat the flow as invalid and stop parsing */
	if(reqn == 0)
		return 1;
		
	/* Second step: find responses */
	http_pair_t *tmp = f->http_f;
	http_pair_t *found_http = NULL;
	seq = f->order->dst;
	if( seq != NULL)
		seq_next = seq->next;
	else
		seq_next = NULL;		/* NULL */
	if(fin_seq != NULL && seq != NULL){
		/*There is FIN packet.*/
		while(compare_sequence_time(seq, fin_seq) < 0)
		{
			pkt = seq->pkt;
			if ( pkt != NULL && pkt->http == HTTP_RSP)
			{
				/*
				 * Similar to the request parsing, a new response is
				 * added to the first pair without response
				 */
				rspn++;
				/* Try to find the first pair without response */
				while(tmp != NULL)
				{
					if(tmp->response_header == NULL)
						break;
					tmp = tmp->next;
				}
				if(tmp == NULL)
					/* no response empty, then return */
					return 1;
				else
				{
					/*Found!*/
					found_http = tmp;
					first_seq = seq;
					found_http->rsp_fb_sec = seq->cap_sec;
					found_http->rsp_fb_usec = seq->cap_usec;
					rsp = http_response_new();
					http_add_response(found_http, rsp);
					http_parse_response(rsp, pkt->tcp_odata, pkt->tcp_odata + pkt->tcp_dl);
				}
			}
			else
			{
				if(found_http == NULL)
				{
					seq = seq->next;
					if(seq != NULL)
						seq_next = seq->next;
					else
						break;
					continue;
				}
			}

			if ( found_http != NULL )
			{
				/*first_seq != NULL*/
				if( seq_next == NULL || seq_next == fin_seq || seq_next->pkt != NULL ||\
						compare_sequence_time(seq_next, fin_seq) >= 0 )
				{
					found_http->rsp_lb_sec = seq->cap_sec;
					found_http->rsp_lb_usec = seq->cap_usec;
					//assert( seq->nxt_seq != 0 );
					if(seq->nxt_seq != 0){
						found_http->rsp_total_len = seq->nxt_seq - first_seq->seq;
						//printf("%d,%d", found_http->rsp_total_len, found_http->response_header->hdlen);
						//assert(found_http->rsp_total_len >= found_http->response_header->hdlen);
						found_http->rsp_body_len = found_http->rsp_total_len - found_http->response_header->hdlen;
					}
					/*Update flow's last byte time.*/
					if ((seq->cap_sec > f->lb_sec) || (seq->cap_sec == f->lb_sec && seq->cap_usec > f->lb_usec))
					{
						f->lb_sec = seq->cap_sec;
						f->lb_usec = seq->cap_usec;
					}
				}
				else
				{
					//assert(seq->seq <= seq_next->seq);
				}
			}

			seq = seq->next;
			if(seq != NULL)
				seq_next = seq->next;
			else
				break;
		}
	}
	else
	{
		/*There is no FIN packet.*/
		while(seq != NULL)
		{
			pkt = seq->pkt;
			if ( pkt != NULL && pkt->http == HTTP_RSP )
			{
				/*
				 * Similar to the request parsing, a new response is
				 * added to the first pair without response
				 */
				rspn++;
				/* Try to find the first pair without response */
				while(tmp != NULL)
				{
					if(tmp->response_header == NULL)
						break;
					tmp = tmp->next;
				}
				if(tmp == NULL)
					/* no response empty, then return */
					return 1;
				else
				{
					/*Found!*/
					found_http = tmp;
					first_seq = seq;
					found_http->rsp_fb_sec = seq->cap_sec;
					found_http->rsp_fb_usec = seq->cap_usec;
					rsp = http_response_new();
					http_add_response(found_http, rsp);
					http_parse_response(rsp, pkt->tcp_odata, pkt->tcp_odata + pkt->tcp_dl);
				}
			}
			else
			{
				if(found_http == NULL)
				{
					seq = seq->next;
					if(seq != NULL)
						seq_next = seq->next;
					else
						break;
					continue;
				}
			}

			if ( found_http != NULL )
			{
				/*first_seq != NULL*/
				if( seq_next == NULL || seq_next->pkt != NULL )
				{
					found_http->rsp_lb_sec = seq->cap_sec;
					found_http->rsp_lb_usec = seq->cap_usec;
					//assert( seq->nxt_seq != 0 );
					if(seq->nxt_seq != 0){
						found_http->rsp_total_len = seq->nxt_seq - first_seq->seq;	
						assert(found_http->rsp_total_len >= found_http->response_header->hdlen);
						found_http->rsp_body_len = found_http->rsp_total_len - found_http->response_header->hdlen;
					}
					/*Update flow's last byte time.*/
					if ((seq->cap_sec > f->lb_sec) || (seq->cap_sec == f->lb_sec && seq->cap_usec > f->lb_usec))
					{
						f->lb_sec = seq->cap_sec;
						f->lb_usec = seq->cap_usec;
					}
				}
				else
				{
					//assert(seq->seq <= seq_next->seq);
				}
			}

			seq = seq->next;
			if(seq != NULL)
				seq_next = seq->next;
			else
				break;
		}
	}
	return 0;
}