Ejemplo n.º 1
0
int http_serve_file ( http_request *h, const gchar *docroot ) {
    gchar *path;
    guint fd, status;

    path = http_fix_path( h->uri, docroot );
    fd   = http_open_file( path, &status );

    http_add_header(  h, "Content-Type", http_mime_type( path ) );
    http_send_header( h, status, fd == -1 ? "Not OK" : "OK" );

    if ( fd != -1 )
	http_sendfile( h, fd );

    close(fd);
    g_free(path);
    return ( fd != -1 );
}
Ejemplo n.º 2
0
void http_tcpapp(unsigned int idx, char *rx, unsigned int rx_len, unsigned char *tx)
{
  unsigned int tx_len;

  DEBUGOUT("HTTP: TCP app\n");

  switch(http_table[idx].status)
  {
    case HTTP_CLOSED: //new connection
      if(rx_len)
      {
        if(strncmpi(rx, "GET", 3) == 0)
        {
          rx     += 3+1;
          rx_len -= 3+1;
          http_sendfile(idx, rx, tx);
        }
        else if(strncmpi(rx, "POST", 4) == 0)
        {
          rx     += 4+1;
          rx_len -= 4+1;
          if(strncmpi(rx, "/station", 8) == 0)
          {
            http_table[idx].status = HTTP_STATION;
            rx = (char*)http_skiphd(rx, &rx_len);
          }
          else if(strncmpi(rx, "/alarm", 6) == 0)
          {
            http_table[idx].status = HTTP_ALARM;
            rx = http_skiphd(rx, &rx_len);
          }
          else if(strncmpi(rx, "/settings", 9) == 0)
          {
            http_table[idx].status = HTTP_SETTINGS;
            rx = http_skiphd(rx, &rx_len);
          }
          else
          {
            http_sendfile(idx, rx, tx);
          }
        }
        else
        {
          tx_len = sprintf((char*)tx, HTTP_400_HEADER"Error 400 Bad request\r\n\r\n");
          tcp_send(idx, tx_len, 0);
          tcp_close(idx);
        }
      }
      break;

    case HTTP_SEND:
      http_sendfile(idx, 0, tx);
      break;
  }

  //parse station, alarm or settings
  switch(http_table[idx].status)
  {
    case HTTP_STATION:
      if(rx_len)
      {
        http_station(rx, rx_len);
        http_sendfile(idx, "/station", tx);
      }
      else
      {
        tcp_send(idx, 0, 0);
      }
      break;

    case HTTP_ALARM:
      if(rx_len)
      {
        http_alarm(rx, rx_len);
        http_sendfile(idx, "/alarm", tx);
      }
      else
      {
        tcp_send(idx, 0, 0);
      }
      break;

    case HTTP_SETTINGS:
      if(rx_len)
      {
        http_settings(rx, rx_len);
        http_sendfile(idx, "/settings", tx);
      }
      else
      {
        tcp_send(idx, 0, 0);
      }
      break;
  }

  return;
}
void http_engine(unsigned int idx, char *rx, unsigned int rx_len, unsigned char *tx)
{	
    unsigned int tx_len = 0;
	static unsigned int updFlag=0;
	  
    switch (http_table[idx].status)
    {
        case HTTP_CLOSED:
            if (rx_len)
            {			    
                if(strncmpi(rx, "GET", 3) == 0)/*METHOD/part-to-resource-HTTP/Version-number*/
                {
					updFlag=0;
					log_info("GET Table form data.\n");
                    rx     += 3+1;
                    rx_len -= 3+1;
					if (strncmpi(rx, "/login.html", strlen("/login.html")) == 0)
					{
						log_info("Login system.\n");
						http_sendfile(idx, "/login.html", tx);
					} 
					else if (strncmpi(rx, "/status.html", strlen("/status.html")) == 0) 
					{
					 	log_info("Get system status html.\n");
						http_sendfile(idx, "/status.html", tx);
					}
					else if (strncmpi(rx, "/wifibase.html", strlen("/wifibase.html")) == 0) 
					{
					 	log_info("Get wifibase html.\n");
						http_sendfile(idx, "/wifibase.html", tx);
					}
					else if (strncmpi(rx, "/wifinet.html", strlen("/wifinet.html")) == 0) 
					{
					 	log_info("Get wifi network html.\n");
						http_sendfile(idx, "/wifinet.html", tx);
					}
					else if (strncmpi(rx, "/public.css", strlen("/public.css")) == 0) 
					{
					 	log_info("Get public css.\n");
						http_sendfile(idx, "/public.css", tx);
					}
					else 
					{
						log_info("Default login webserver.\n\n");
                    	http_sendfile(idx, "/login.html", tx);
					}
                }
                else if(strncmpi(rx, "POST", 4) == 0)
                {
					log_info("POST Table form data.\n");
					updFlag=0;
                    rx     += 4+1;
                    rx_len -= 4+1;
                    if (strncmpi(rx, "/reboot", 7) == 0)		/* process setupwifi form */
                    {
                        tx_len = sprintf((char*)tx, HTTP_200_HEADER);
						http_resetsys(idx, (char*)tx, tx_len);												         
                    }
				    else if (strncmpi(rx, "/factory", 8) == 0)  /* process resetwifi form */
					{
                        tx_len = sprintf((char*)tx, HTTP_200_HEADER);
						http_factory(idx, (char*)tx, tx_len);						
					}
					else if (strncmpi(rx, "/update", 7) == 0)  /* process resetwifi form */
					{	  
						updFlag=1;  
					}
				    else if (strncmpi(rx, "/wifibase", 9) == 0)  /* process resetwifi form */
					{
						http_setup_wifibase(idx, (char*)rx, rx_len, (char *)tx);
					}
				    else if (strncmpi(rx, "/setupuart", 10) == 0)  /* process resetwifi form */
					{
						http_setup_uart(idx, (char*)rx, rx_len, (char *)tx);
					}
				    else if (strncmpi(rx, "/setupnet", 9) == 0)  /* process resetwifi form */
					{
						http_setup_net(idx, (char*)rx, rx_len, (char *)tx);
					}
                    else
                    {
                        http_sendfile(idx, rx, tx);
                    }
                }
				else if(updFlag != 0)
				{					
					if(http_update(idx, (char*)rx, rx_len, (char *)tx, updFlag) == 0)
					{
						updFlag=0;
						printf("rebooting....\r\n");
						BSP_ChipReset();
					}
					else
						updFlag++;		
				}
                else
                {
                    tx_len = sprintf((char*)tx, HTTP_400_HEADER"Error 400 Bad request\r\n\r\n");
                    tcp_send(idx, tx_len);
                }
            }
            break;

        case HTTP_SEND:
            http_sendfile(idx, 0, tx);
            break;

        default:                                                      
            break;                                                          
    }                                                                         

    return;
}
Ejemplo n.º 4
0
static void work_http( connection* conn )
{
	int i;
	virtual_host * host;
	//check if the connection cannot work.
	if( conn->state != C_READY )
		return;
	conn->state = C_REQUESTING;
	http_request( conn );
	if( conn->state != C_REQUESTING )
		return;
	//response
	conn->code = 200;
	conn->state = C_RESPONSING;
	/* Check Host and then set root directory. */
	host = loop_search( &conn->server->loop_vhost, conn->host, vhost_searcher );
	if( !host ){
		host = loop_search( &conn->server->loop_vhost, "*", vhost_searcher );
	}
	if( host ){
		//read root
		conn->root_dir = host->root_dir;
		if( !loop_is_empty( &host->loop_rewrite ) )
			loop_search( &host->loop_rewrite, (void*)conn, loop_rewrite_match );
		http_parse_uri( conn );
		DBG("[%s]%s%s", conn->client->ip_string, conn->host, conn->uri );
_RESPONSE:	
		http_parse_path( conn );
		conn->document_type = http_doctype( conn->server, conn->extension );
		if( !conn->document_type ){
			http_error( conn, 404, "<h1>File not found.</h1>" );
		}else if( conn->extension[0] &&
			strstr( conn->server->asp_exts, conn->extension ) )
		{
			//php  do ...
			exec_asp( conn );
		}else if( host->proxy && ( !host->proxy_exts[0] ||
			strstr( host->proxy_exts, conn->extension ) ) )
		{
			// uses proxy server
			proxy_request( conn, host->proxy_ip, host->proxy_port );
		}else if( access(conn->full_path, 0)==0 ){
			if( is_dir(conn->full_path) ){
				char* tmp;
				NEW( tmp, PATH_LEN+32 );
				if( conn->script_name[strlen(conn->script_name)-1] != '/' ){
					//Are you sure that script starts with '/'?
					sprintf( tmp, "http://%s%s/", conn->host, conn->script_name );  
					http_redirect( conn, tmp );
				}else{
					if( tmp ){
						for( i = 0; i<10; i++ )
						{
							if( !conn->server->default_pages[i][0] ) {
								i=10;
								break;
							}
							sprintf( tmp, "%s/%s", conn->full_path, conn->server->default_pages[i] );
							if( access( tmp, 0 ) == 0 )
							{
								//091004 by Huang Guan.
								sprintf( conn->script_name, "%s%s", conn->script_name, 
									conn->server->default_pages[i] );
								DEL( tmp );
								goto _RESPONSE;
							}
						}
					}
					if( i == 10 ){
						// List Directory
						if( host->list ){
							int ret;
							NEW( conn->data_send, MAX_DATASEND+4 );
							strcpy( conn->extension, "html" );
							conn->document_type = http_doctype( conn->server, conn->extension );
							ret = listdir( conn->data_send, MAX_DATASEND, conn->full_path, 
								conn->script_name );
							conn->data_size = ret;
						}else{
							http_error( conn, 403, "<h1>Forbidden</h1>" );
						}
					}
				}
				DEL( tmp );
			}else{
				http_sendfile( conn, conn->full_path );
			}
		}else if( strncmp(conn->current_dir, "/system", 7)==0 && conn->root_dir==host->root_dir ){
			strcpy(conn->script_name, conn->script_name+7);
			conn->root_dir = conn->client->server->root_dir;
			goto _RESPONSE;
		}else{
			http_error( conn, 404, "<h1>File not found.</h1>" );
		}
	}else{
		http_error( conn, 403, "<h1>Unknown host name.</h1>" );
	}

	if( conn->state == C_RESPONSING )
		http_response( conn );
	conn->requests ++;
	if( conn->form_data )
		DEL( conn->form_data );
	if( conn->data_send )
		DEL( conn->data_send );
	
	if( conn->session )
		conn->session->reference --;
	conn->session = NULL;
		
	//next request
	if( conn->keep_alive ){
		conn->state = C_READY;
	}else{
		conn->state = C_END;
	}
}