Example #1
0
/** Send a complete HTTP request, with all HTTP headers prebuilt.
    Returns HTTP status. 
*/
int osl::http_connection::send(const std::string &data)
{
	p.status(1,"Sending HTTP request to "+host);
	p.status(3,"HTTP request data: "+data);
	skt_sendN(s,&data[0],data.size());
	
	p.status(1,"Waiting for HTTP response from "+host);
	std::string status=skt_recv_line(s);
	p.status(2,"HTTP response status: "+status);
	unsigned int i=0; while (status[i]!=' ' && i<status.size()) i++;
	std::string codeDesc=status.substr(i,std::string::npos);
	int code=0;
	sscanf(codeDesc.c_str(),"%d",&code);
	
	std::string l;
	while (0!=(l=skt_recv_line(s)).size()) 
	{   /* ^ a zero-length line indicates the end of the HTTP headers */
		p.status(3,"HTTP response header line: "+l);
		int firstColon=l.find_first_of(":");
		std::string keyword=l.substr(0,firstColon);
		std::string value=l.substr(firstColon+2,std::string::npos);
		header[keyword]=value;
	}
	
	return code;
}
Example #2
0
// Main network access function:
void handle_client(SOCKET s, bool intranet) {
	std::string status="Server ";
	status+=get_address(&status);
	status+=": ";
	if (working_OK) status+="TOTALLY OK";
	else status+="not really ok";

	status+=", hackability=";
	if (is_hackable) status+="high";
	else status+="not so much";
	
	status+=".  Your name?\n";
	skt_sendN(s,&status[0],status.size());
	
	std::string name=skt_recv_line(s);
	if (is_hackable && intranet) {
		old_say_hello(&name[0]);
	} else {
		std::cout<<"Hello, "<<name<<"\n";
	}
	
	skt_close(s);
}
Example #3
0
unsigned char *download_url(const char *url_in, int verbose, int *length)
{
  char *url=STRDUP(url_in);

  // parse the url
  char *protocol, *host, *path;
  int port;
  parse_url(url, &protocol, &host, &port, &path);

  if (verbose)
    printf("Connecting to URL: %s://%s:%d%s\n", protocol, host, port, path);

  int timeout = 60;
  if (verbose)
    printf("Looking up IP Address for: %s\n", host);
  skt_ip_t hostIP = skt_lookup_ip(host);
  if (verbose)
    printf("Connecting to %s\n", host);
  SOCKET s = skt_connect(hostIP, port, timeout);

  char *send_get = MALLOC(strlen(url)+128);
  sprintf(send_get,
          "GET %s HTTP/1.1\r\n"
          "Host: %s\r\n"
          "User-Agent: Mozilla/5.0\r\n"
          "\r\n", path, host);

  // send our get request
  skt_sendN(s, send_get, strlen(send_get));

  // wait...
  if (verbose)
    printf("Waiting for a response from %s\n", host);
  char *status = skt_recv_line(s);
  if (verbose)
    printf("Received status: %s\n", status);
  //char *status_trim = trim_spaces(status);
  free(status);

  // now can get the response code
  //int code = atoi(status_trim);
  //free(status_trim);

  // a zero-length line indicates the end of the HTTP headers... we ignore
  int len=-1;
  while (1) {
    char *line = skt_recv_line(s);
    if (strlen(line)==0) break;
    if (strstr(line, "Content-Length") != 0) {
      char *l = line + strlen("Content-Length") + 2;
      len=atoi(l);
    }
  }
  if (verbose)
    printf("Content Length: %d\n", len);

  if (len==-1) {
    asfPrintWarning("No Content-Length specified in the HTTP headers.\n");
    return NULL;
  }

  // receiving data...
  unsigned char *data = CALLOC(len+12, sizeof(char));
  int curr=0, chunkSize=1024;
  while (1) {
    int amt = chunkSize < len-curr ? chunkSize : len-curr;
    skt_recvN(s,data+curr,amt);
    curr += amt;
    if (verbose)
      printf("Retrieved %d Kb so far.\n", curr);
    if (curr==len)
      break;
    else if (curr>len)
      asfPrintError("Invalid Content-Length?\n");
  }

  if(verbose)
    printf("Done.\n");
  //if (verbose)
  //  printf("Received data:\n"
  //         "-------------------------------------------------------------\n"
  //         "%s\n"
  //         "-------------------------------------------------------------\n",
  //         data);

  free(protocol);
  free(host);
  free(path);
  free(url);
  free(send_get);
  skt_close(s);

  *length = len;
  return data;
}