Example #1
0
http_response::http_response(socket_stream* client)
{
	debug_ = false;
	header_ok_ = false;
	head_sent_ = false;
	client_ = NEW http_client(client);
}
int main(int argc, char *argv[])
{
  // Get a hostname and uri from the user (assume default http port)
  if (argc <= 2)
  {
    std::cout << "Usage: simple_http_client [host] [uri]\n"
              << "E.g. simple_http_client www.boost.org /LICENSE_1_0.txt"
              << std::endl;
    return 1;
  }

  std::string host_name(argv[1]);
  std::string uri(argv[2]);
  std::cout << "HTTP client host: " << host_name
            << " uri: " << uri << std::endl;
  try
  {
    // create an io_service for the tcp port
    boost::asio::io_service io_service;

    // create an http_client
    http_client_type::shared_pointer http_client
        (http_client_type::create(io_service));

    // connect the response and chunk received callback functions
    http_client->response_received_event(response_receiver);
    http_client->chunk_received_event(chunk_receiver);

    // attempt to connect to the host on the standard http port (80)
    if (http_client->connect(host_name))
    {
      // create an http request
      // Note: adds a "connection close" header since we're only sending
      // one request, so we'll get the server to close the connection after
      // it's response
      via::http::tx_request request(via::http::request_method::GET, uri);
      request.add_header(via::http::header_field::CONNECTION, "close");

      // send the request to the host.
      http_client->send(request);

      // run the io_service to start communications
      io_service.run();
    }
    else
    {
      std::cout << "Could not resolve host: " << host_name << std::endl;
      return 1;
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception:"  << e.what() << std::endl;
  }

  return 0;
}
Example #3
0
/**
 * scout main
 */  
int 
main( int argc, char *argv[] )
{
  int x, result;                /* index, func. result*/
  struct sigaction  action;   
  CLIENT           *client;     /* defined in setup.h */  
 
  init_config();                /* defined in init.h */
  parse_cmdline(argc, argv);    /* defined above     */
  
  if( my.config ){
    /* see: init.h */  
    show_config( TRUE );    
  }

  memset( &action, 0, sizeof( action ));
  action.sa_handler = signal_handler;
  if( sigaction( SIGHUP, &action, NULL ))
    joe_error( "sigaction" );
  if( sigaction( SIGINT, &action, NULL))
    joe_error( "sigaction" );
  if( sigaction( SIGTERM, &action, NULL))
    joe_error( "sigaction" );

  /* cookie is an EXTERN, defined in setup */ 
  cookie = (COOKIE*)malloc( sizeof(COOKIE)); 
  if( !cookie ){ joe_fatal( "memory exhausted" ); }
  cookie->first = NULL;
  client = (CLIENT*)malloc(sizeof(CLIENT));
  if( !client ){ joe_fatal( "application memory exhausted" ); } 

  http_client( client );
  write_queue( my.showcodes );
  /**
   * exit program.
   */
  exit( EXIT_SUCCESS );	
} /* end of int main **/
Example #4
0
int main(int argc, char *argv[])
{

  struct sockaddr_in server;
  struct timeval start_time,end_time,mud_time;
  int sock;
  char deststr[80] = "db1.ertl.jp";
  unsigned int **addrptr;

  gettimeofday(&start_time,NULL);

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    return 1;
  }

  server.sin_family = AF_INET;
  server.sin_port = htons(5555); /* HTTPのポートは80番です */

  server.sin_addr.s_addr = inet_addr(deststr);
  if (server.sin_addr.s_addr == 0xffffffff) {
    struct hostent *host;

    host = gethostbyname(deststr);
    if (host == NULL) {
      if (h_errno == HOST_NOT_FOUND) {
	/* h_errnoはexternで宣言されています */
	printf("host not found : %s\n", deststr);
      } else {
	/*
	  HOST_NOT_FOUNDだけ特別扱いする必要はないですが、
	  とりあえず例として分けてみました
	*/
	printf("%s : %s\n", hstrerror(h_errno), deststr);
      }
      return 1;
    }

    addrptr = (unsigned int **)host->h_addr_list;

    while (*addrptr != NULL) {
      server.sin_addr.s_addr = *(*addrptr);

      /* connect()が成功したらloopを抜けます */
      if (connect(sock,
		  (struct sockaddr *)&server,
		  sizeof(server)) == 0) {
	break;
      }

      addrptr++;
      /* connectが失敗したら次のアドレスで試します */
    }

    /* connectが全て失敗した場合 */
    if (*addrptr == NULL) {
      perror("connect");
      return 1;
    }
  } else {
    if (connect(sock,
		(struct sockaddr *)&server, sizeof(server)) != 0) {
      perror("connect");
      return 1;
    }
  }

  gettimeofday(&mud_time,NULL);

  http_client(sock);

  close(sock);

  gettimeofday(&end_time,NULL);

  printf("%f\n",(double)(mud_time.tv_usec-start_time.tv_usec));
  printf("%f\n",(double)(end_time.tv_usec-start_time.tv_usec));


  return 0;
}
Example #5
0
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{  
	uint16 i; 
        RCC_Configuration(); // Configure the system clocks
	NVIC_Configuration(); // NVIC Configuration
	GPIO_Configuration();
	USART1_Init(115200);
	Timer_Configuration();
        
        printf("============================================\r\n");
	printf("   HTTP client for W5200 \r\n");
	printf("============================================\r\n");
       
	WIZ_SPI_Init();	
        Reset_W5200();
        wizInit();
        InitNetInfo();
        DisplayNetInfo();
	              
        // LED3 and LED4 On!
	LED3_onoff(ON);
	LED4_onoff(ON);
     
       
        uint8  url[MAX_URL_SIZE];			//full url
	uint8  url_dn[MAX_URL_SIZE];			//domain name url
	uint8  url_path[MAX_URL_SIZE];		//local path in html server
	char * split;				//string split pointer 
	
	
	uint16 done_http = 0;
	uint16 start_idx = 0 ;
	uint16 end_idx = 0;
	uint16 tmp_start;
	uint16 tmp_end;
	uint8 BR [4]= "<BR>";
	uint8 rBR [4]= "\r\n";
	uint8 TITLE [7]= "<TITLE>";
	uint8 bTITLE [8]= "</TITLE>";
	

	uint16 tmp_idx;
	uint8 no_pr = 0;
	uint8 str[17];
       
	
            while (1)
            {
                          
              sprintf((char*)str,"%.3d.%.3d.%.3d.%.3d ",
				IINCHIP_READ (SIPR0+0), IINCHIP_READ (SIPR0+1), 
				IINCHIP_READ (SIPR0+2), IINCHIP_READ (SIPR0+3));
                            
                           
                            /* Get Http Address  */
                            printf("\r\n Please enter a HTTP Address without 'http://' \n\r"); 
                            printf("http://");
                            memset(url,0,sizeof(url));	
                            //Modified by Gang 2011-10-04
                            zScanf_s(1, url);
                            printf("\r\n Your HTTP address is: %s \r\n",url);		
                            
                            /* Parse URL Path  */
                            split = strchr((char const*)url,'/');			
                            strcpy((char*)url_path,split); 			
                            printf("Domain path: %s \r\n",url_path);
                    
                            /* Parse URL Domain  */
                            split = strtok((char*)url,"/");			
                            strcpy((char*)url_dn,split);				
                            printf("Domain name: %s \r\n",url_dn);
                            
               
                            /* Do DNS Client */
                            memset(httpclient_NetInfo.HTTPs_IP,0,sizeof(httpclient_NetInfo.HTTPs_IP));
                            //Delay_ms(10);
                            done_dns = dns_query(DNS_SOCK, url_dn, httpclient_NetInfo.HTTPs_IP);
                            printf("\r\n HTTPs_IP= %d.%d.%d.%d",httpclient_NetInfo.HTTPs_IP[0],httpclient_NetInfo.HTTPs_IP[1],httpclient_NetInfo.HTTPs_IP[2],httpclient_NetInfo.HTTPs_IP[3]);
                          
                            while(done_dns) {
                            
                            /* Do HTTP Client */
                            done_http = http_client(HTTPC_SOCK, httpclient_NetInfo.HTTPs_IP, url_path, url_dn,data_buf);
                           
                            if(done_http) { // on success, done_dns is not  '0'
                                            
    #define Recieved_DATA
    #ifdef Recieved_DATA
                                            printf("\r\n<< Recieved Data -- START>> \r\n");
                                           
                                            for(i=0; i<done_http; i++){
                                                  
                                                  printf("%c",(uint8)data_buf[i]);
                                              }   
                                            printf("\r\n");
                                            printf("<< Recieved Data -- END>> \r\n");
                                           
    #endif
                                        /* parsed index */
                                            //All other HTML elements are nested between the opening <html> and </html> tags.
                                            start_idx = pased_idx((uint8 *)str_start , sizeof(str_start), done_http );
                                            end_idx   = pased_idx((uint8 *)str_end , sizeof(str_end), done_http );			
    
                                            /* printf get <html> ...</html> */							
                                            for(i=start_idx; i<(end_idx+7); i++){						
                                                    /* remove header */
                                                    data_buf[i-start_idx] = data_buf[i];
                                             }   
                                            printf("\r\n");
                                            
    
                                            /* replace <br> tag to \r\n */
                                            //The br tag is used for specifying a line break.
                                            do{
                                                    tmp_idx = pased_idx((uint8*)BR, sizeof(BR) , end_idx-start_idx) ;
                                                    if(tmp_idx == 0 ) 
                                                            break;
                                                 
                                                    memcpy((uint8 *)data_buf+tmp_idx, (uint8*)rBR, sizeof(rBR)) ;
                                            }while(tmp_idx!=end_idx-start_idx);
    
    #define Parsed_DATA
    #ifdef Parsed_DATA
                                            /* parsed DATA */
                                            printf("\r\n<< Parsed Data -- START >>");
                                            printf("\r\nTITLE : \r\n");
                                            /* parse <TITLE> and </TITLE> tags */
                                            tmp_start = pased_idx((uint8 *)TITLE , sizeof(TITLE),(end_idx-start_idx) ) + sizeof(TITLE);
                                            tmp_end   = pased_idx((uint8 *)bTITLE , sizeof(bTITLE),(end_idx-start_idx) );												
                                            for(i=tmp_start; i<tmp_end; i++){
                                                   
                                                    printf("%c",(uint8)data_buf[i]); // printf title
                                            }   
    
                                            printf("\r\n BODY : \r\n");
                                            /*DO NOT PRINT TAG COMMAND: between '<' with '>' */
                                            for(i=tmp_end; i<(end_idx-start_idx); i++){
                                                    //Tag command - ex.)<PRE>
                                                    //'<' is a start point of tag command.
                                                    if((uint8)data_buf[i]=='<'){ 
                                                            no_pr = 0;
                                                    }
                                                    //'>' is a end point of Tag command.
                                                    //To avoid in row tags -> ex.) <PRE><H1>						
                                                    if((uint8)data_buf[i]=='>' && (uint8)data_buf[i+1] !='<' ) {
                                                            no_pr = 1;
                                                            i++;
                                                    }					
                                                    if(no_pr){
                                                        Delay_ms(1);    
                                                        printf("%c",(uint8)data_buf[i]);
                                                    }
                                            }   
                                            printf("\r\n<< Parsed Data -- END >>\r\n");
    #endif
                                            /* Init. parameter */
                                            start_idx= 0;
                                            end_idx= 0;				
                                    done_dns = 0;  
                                    break;
    
                            }
                           //done_http
                            //done_dns=0;
                    }//while : done_dns == 1		
            }
}