/*
 * http_handle_post()
 *
 * Process the post request and take the appropriate action.
 */
int http_handle_post(http_conn* conn)
{
  alt_u8* tx_wr_pos = conn->tx_buffer;
  int ret_code = 0;
  struct upload_buf_struct *upload_buffer = &upload_buf;
  
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_VERSION_STRING);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_NO_CONTENT_STRING);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_CLOSE);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_END_OF_HEADERS);

  if (!strcmp(conn->uri, mapping.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    mapping.func();
  }

  else if (!strcmp(conn->uri, sweep_field.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    sweep_field.func(conn);
  }
  
  else if (!strcmp(conn->uri, seg_field.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    seg_field.func(conn);
  }
  
  else if (!strcmp(conn->uri, lcd_field.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    lcd_field.func(conn);
  }

  else if (!strcmp(conn->uri, upload_field.name))
  {
    conn->file_upload = 1;
    upload_buffer->rd_pos = upload_buffer->wr_pos = (alt_u8*) upload_buffer->buffer;
    memset(upload_buffer->rd_pos, '\0', conn->content_length );
    upload_field.func(conn);
  }
  else if (!strcmp(conn->uri, flash_field.name))
  {
    /* Kick off the flash programming. */
    flash_field.func( conn );
  }
#ifdef RECONFIG_REQUEST_PIO_NAME
  else if (!strcmp(conn->uri, reset_field.name))
  {
    /* Close the socket. */
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    reset_field.func();
  }
#endif  
  return ret_code;
}
/*
 * http_handle_receive()
 *
 * Work out what the request we received was, and handle it.
 */
void http_handle_receive(http_conn* conn, int http_instance)
{
  int data_used, rx_code;
  
  if (conn->state == READY)
  {
    rx_code = recv(conn->fd, conn->rx_wr_pos, 
              (HTTP_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1), 
              0);
        
    /* 
     * If a valid data received, take care of buffer pointer & string 
     * termination and move on. Otherwise, we need to return and wait for more
     * data to arrive (until we time out).
     */
    if(rx_code > 0)
    {
      /* Increment rx_wr_pos by the amount of data received. */
      conn->rx_wr_pos += rx_code;
      /* Place a zero just after the data received to serve as a terminator. */
      *(conn->rx_wr_pos+1) = 0;
      
      if(strstr(conn->rx_buffer, HTTP_END_OF_HEADERS))
      {
        conn->state = PROCESS;
      }
      /* If the connection is a file upload, skip right to DATA.*/
      if(conn->file_upload == 1)
      {
        conn->state = DATA;
      }
    }
  }
  
  if(conn->state == PROCESS)
  {
    /* 
     * If we (think) we have valid headers, keep the connection alive a bit
     * longer.
     */
    conn->activity_time = alt_nticks();
    
    /* 
     * Attempt to process the fundamentals of the HTTP request. We may 
     * error out and reset if the request wasn't complete, or something
     * was asked from us that we can't handle.
     */
    if (http_process_request(conn))
    {
      fprintf(stderr, "[http_handle_receive] http_process_request failed\n");
      conn->state = RESET;
      http_manage_connection(conn, http_instance);
    }
    
    /* 
     * Step through the headers to see if there is any other useful 
     * information about our pending transaction to extract. After that's 
     * done, send some headers of our own back to let the client know 
     * what's happening. Also, once all in-coming headers have been parsed
     * we can manage our RX buffer to prepare for the next in-coming 
     * connection.
     */
    while(conn->state == PROCESS)
    {
      if(http_read_line(conn))
      {
        fprintf(stderr, "[http_handle_receive] error reading headers\n");
        conn->state = RESET;
        http_manage_connection(conn, http_instance);
        break;
      }
      if(http_process_headers(conn))
      {
        if( (conn->rx_rd_pos = strstr(conn->rx_rd_pos, HTTP_CR_LF)) )
        {
          conn->rx_rd_pos += 2;
          conn->state = DATA;
          conn->activity_time = alt_nticks();
        }
        else
        {
          fprintf(stderr, "[http_handle_receive] Can't find end of headers!\n");
          conn->state = RESET;
          http_manage_connection(conn, http_instance);
          break;
        }
      } 
    } /* while(conn->state == PROCESS) */
    
    if( http_prepare_response(conn) )
    {
      conn->state = RESET;
      fprintf(stderr, "[http_handle_receive] Error preparing response\n");
      http_manage_connection(conn, http_instance);
    }
              
    /* 
     * Manage RX Buffer: Slide any un-read data in our input buffer 
     * down over previously-read data that can now be overwritten, and 
     * zero-out any bytes in question at the top of our new un-read space. 
     */
    if(conn->rx_rd_pos > (conn->rx_buffer + HTTP_RX_BUF_SIZE))
    {
      conn->rx_rd_pos = conn->rx_buffer + HTTP_RX_BUF_SIZE;
    }
        
    data_used = conn->rx_rd_pos - conn->rx_buffer;
    memmove(conn->rx_buffer,conn->rx_rd_pos,conn->rx_wr_pos-conn->rx_rd_pos);
    conn->rx_rd_pos = conn->rx_buffer;
    conn->rx_wr_pos -= data_used;
    memset(conn->rx_wr_pos, 0, data_used);
   }
   
  if (conn->state == DATA && conn->file_upload == 1 )
  {
    /* Jump to the file_upload() function....process more received data. */
    upload_field.func(conn);
  }
}
Exemple #3
0
/*
 * http_handle_post()
 *
 * Process the post request and take the appropriate action.
 */
int http_handle_post(http_conn* conn)
{
  int responseLen = 0;
  const char * postResponse = httpHandlePost(conn, &responseLen);
  if (responseLen != 0){
		http_send_header(conn, responseLen, HTTP_OK, false);
		//Send JSON reply
		send(conn->fd, (void*)postResponse, responseLen, 0);
		conn->state = CLOSE;
		return 0;
  }
  //Fall through to old code if necessary
  char* tx_wr_pos = conn->tx_buffer;
  int ret_code = 0;
  struct upload_buf_struct *upload_buffer = &upload_buf;
  
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_VERSION_STRING);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_NO_CONTENT_STRING);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_CLOSE);
  tx_wr_pos += sprintf(tx_wr_pos, HTTP_END_OF_HEADERS);

  if (!strcmp(conn->uri, mapping.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    mapping.func();
  }

  else if (!strcmp(conn->uri, sweep_field.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    sweep_field.func(conn);
  }
  
  else if (!strcmp(conn->uri, lcd_field.name))
  {
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    conn->state = CLOSE;
    lcd_field.func(conn);
  }

  else if (!strcmp(conn->uri, upload_field.name))
  {
    conn->file_upload = 1;
    upload_buffer->rd_pos = upload_buffer->wr_pos = upload_buffer->buffer;
    memset(upload_buffer->rd_pos, '\0', conn->content_length );
    upload_field.func(conn);
  }
  else if (!strcmp(conn->uri, flash_field.name))
  {
    /* Kick off the flash programming. */
    flash_field.func( conn );
  }
#ifdef RECONFIG_REQUEST_PIO_NAME
  else if (!strcmp(conn->uri, reset_field.name))
  {
    /* Close the socket. */
    send(conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0);
    reset_field.func();
  }
#endif  
  return ret_code;
}