Beispiel #1
0
static int32_t http_request_new(void **pdu, void *args)
{
    int32_t error;
    struct exo_request_args *request_args = args;
    struct string_class *request;

#ifdef TEST_ERSDK
    error = http_request_new_simulate_failure();
    if (error != ERR_SUCCESS)
        return error;
#endif

    error = string_new(&request, "");
    if (error != ERR_SUCCESS) {
        error_log(DEBUG_MEM, ("Failed to create HTTP request\n"), error);
        return error;
    }

    error = http_create_request(request, request_args);
    if (error != ERR_SUCCESS)
        return error;

    *pdu = request;
    return ERR_SUCCESS;
}
Beispiel #2
0
static inline ssize_t
http_method (int fd, Http_destination *dest,
	     Http_method method, ssize_t length)
{
  char str[1024]; /* FIXME: possible buffer overflow */
  Http_request *request;
  ssize_t n;

  if (fd == -1)
    {
      log_error ("http_method: fd == -1");
      return -1;
    }

  n = 0;
  if (dest->proxy_name != NULL)
    n = sprintf (str, "http://%s:%d", dest->host_name, dest->host_port);
  sprintf (str + n, "/index.html?crap=%ld", time (NULL));

  request = http_create_request (method, str, 1, 1);
  if (request == NULL)
    return -1;

  sprintf (str, "%s:%d", dest->host_name, dest->host_port);
  http_add_header (&request->header, "Host", str);

  if (length >= 0)
    {
      sprintf (str, "%d", length);
      http_add_header (&request->header, "Content-Length", str);
    }

  http_add_header (&request->header, "Connection", "close");

  if (dest->proxy_authorization)
    {
      http_add_header (&request->header,
		       "Proxy-Authorization",
		       dest->proxy_authorization);
    }

  if (dest->user_agent)
    {
      http_add_header (&request->header,
		       "User-Agent",
		       dest->user_agent);
    }

  n = http_write_request (fd, request);
  http_destroy_request (request);
  return n;
}
Beispiel #3
0
int proxy_data_coming(event_t *ev, void *arg)
{
    request_t *req;
    connection_t *conn = (connection_t *)arg;
    conn->buf = lt_new_buffer_chain(conn->buf_pool_manager,
            DEFAULT_UPSTREAM_BUFFER_SIZE);
    int rv = lt_recv(conn->fd, conn->buf);//nginx just recv a part
    if (rv == LAGAIN) {
        conn->status = L_PROXY_WAITING_RESPONSE;
    } else if (rv == LCLOSE) {
        debug_print("%s", "proxy FD CLOSE\n");
        conn->status = L_PROXY_CLOSING;
    } else if (rv == LERROR) {
        conn->status = L_PROXY_ERROR;
    }

    if (conn->status == L_PROXY_WAITING_RESPONSE) {
        req = http_create_request(conn);
        http_process_response_line(conn, req);

        debug_print("%s", "SUCCESS parse response\n");
        //send_chains(ev->base, conn->pair->fd, &chain);
//        http_process_response_line(conn, req);
//        http_process_request_line(conn, req);
    } 

    if (conn->status == L_HTTP_WROTE_RESPONSE_HEADER) {
//      if (conn->chunked) {
        int client_fd = conn->pair->fd;
        send_buffers(conn->ev->base, client_fd, conn->buf);
//optional
//A: splice2 in splice2 out
//B: lt_recv() in vmsplice2 out
//C: 
        //http_check_chunked(conn->buf);//TODO
//      }
//        conn->handler(conn, conn->handler_arg);
    }
    //send_chains(ev->base, conn->fd, <#lt_chain_t *#>)
    return 0;
}
int main(void)
{
	ansi_init();
	uart_init();// Initialize UART Peripheral
	IntializeTimer();
  
	// Clear the Screen of the hyper terminal <send clear code>
  
	spi_init();// Initialize SPI

	w5100_init();// Initial the W5100 Ethernet
  
	if(S0_initialize_socket(MR_TCP,30000))
	{
		printf("INIT DONE 2\n");
		//S0_connect(dest_ip,dest_port);
	}	
	
	printf("%x\n",get_socket0_status());
	uart_putch('\0',&uart_str);
	uart_flush();
	
	host= http_extract_host(url);
	relativeAddress = http_extract_relativeAddress(url,strlen(host));
	request= http_create_request(method,relativeAddress,httpV,host);
	;//="GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (windows NT 6.1; wow64) Applewebkit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 safari/537.11\r\nAccept: text/html\r\nAccept-Encoding: gzip, deflate\r\n";
	//printf("%s",request);
	S0_connect(dest_ip,dest_port);
	
	if(get_socket0_status() == SOCK_ESTABLISHED)
	{
		printf("Connected\n");
		uart_putch('\0',&uart_str);
	}
	
  // Loop forever
  int loop = 1, counter = 0;
  //sei();
  while(loop)
  {
	  // keep reading the socket zero status
	  // and handle at least socket closed and socket established
	  
	uint8_t sockstat=get_socket0_status();

	switch(sockstat/*Handle all possible socket status here*/) 
	{
	case SOCK_CLOSED:
		//printf("ERROR1\n");
		//uart_putch('\0',&uart_str);
		//S0_connect(dest_ip,dest_port);
		printf("%x\n",get_socket0_status());
		uart_putch('\0',&uart_str);
		loop = 0;
		break;
	case SOCK_ESTABLISHED:
		printf("SOCK_ESTABLISHED\n");
		uart_putch('\0',&uart_str);
		// Get the client request size
		// And read the client Request in temp buffer
		// make sure the temp buffer is sending GET request
		S0_send((uint8_t*)request,strlen(request));
		printf("Request Sent\n");
		uart_putch('\0',&uart_str);
		uart_flush();
		_delay_ms(2000);
		if(S0_RX_getReceivedSize() != 0)
		{
			counter++;
			S0_recv(buf,S0_RX_getReceivedSize());
			printf("RECV Response\n");
			uart_putch('\0',&uart_str);
			printf("%s",(char*)buf);
			uart_putch('\0',&uart_str);
			//loop =0;
		}
		// create a buffer that will hold the response and send it
		// if the GET request has an option to turn on/off a LED Carry that now.
		// finally disconnect the socket with the client.
		break;
      case SOCK_FIN_WAIT:
      case SOCK_CLOSING:
      case SOCK_TIME_WAIT:
      case SOCK_CLOSE_WAIT:
      case SOCK_LAST_ACK:
		
		loop = 0;
		printf("Recevied %d\n",counter);
		uart_putch('\0',&uart_str);
		close(0);
		// force the socket to be closed
		break;
	//default:	
		//printf("nothing\n");	
	}		
  }
  return 0;
}