net_socket* http_connection::connect(const char* c_url, int port) { char* url = strdup(c_url); // get host name from url // find the first '/' int i, n; for (i = 0, n = (int) strlen(url); url[i] != '/' && i < n; i++); if (i == n) { // '/' is not found fprintf(stderr, "invalid url '%s'\n", url); free(url); return NULL; } tu_string uri = url + i; url[i] = 0; tu_string host = url; free(url); url = NULL; if (net_init() == false) { return NULL; } m_iface = new net_interface_tcp(); // Open a socket to receive connections on. m_ns = m_iface->connect(host.c_str(), port); if (m_ns == NULL) { fprintf(stderr, "Couldn't open net interface\n"); delete m_iface; m_iface = NULL; return NULL; } // connect to HTTP server if (get_proxy_port() > 0) { char buf[512]; snprintf(buf, 512, "CONNECT %s:%d HTTP/1.0\r\n", host.c_str(), port); m_ns->write_string(buf, HTTP_TIMEOUT); m_ns->write_string("User-Agent:gameswf\r\n", HTTP_TIMEOUT); m_ns->write_string("Connection:Keep-Alive\r\n", HTTP_TIMEOUT); m_ns->write_string("\r\n", HTTP_TIMEOUT); if (read_response() == false) { fprintf(stderr, "Couldn't connect to '%s' through proxy '%s'\n", host.c_str(), get_proxy()); delete m_ns; m_ns = NULL; return NULL; } } // We use HTTP/1.0 because we do not wont get chunked encoding data m_ns->write_string(tu_string("GET ") + uri + tu_string(" HTTP/1.0\r\n"), HTTP_TIMEOUT); m_ns->write_string(tu_string("Host:") + host + tu_string("\r\n"), HTTP_TIMEOUT); // m_ns->write_string("Accept:*\r\n", HTTP_TIMEOUT); // m_ns->write_string("Accept-Language: en\r\n", HTTP_TIMEOUT); // m_ns->write_string("Accept-Encoding: gzip, deflate, chunked\r\n", HTTP_TIMEOUT); // m_ns->write_string("Accept-Encoding: *\r\n", HTTP_TIMEOUT); // m_ns->write_string("Proxy-Authenticate:prg\r\n", HTTP_TIMEOUT); // m_ns->write_string("Proxy-Authorization:123\r\n", HTTP_TIMEOUT); m_ns->write_string("User-Agent:gameswf\r\n", HTTP_TIMEOUT); m_ns->write_string("Connection:Close\r\n", HTTP_TIMEOUT); m_ns->write_string("\r\n", HTTP_TIMEOUT); if (read_response() == false) { fprintf(stderr, "Couldn't find resource '%s', host '%s'\n", uri.c_str(), host.c_str()); delete m_ns; m_ns = NULL; return NULL; } return m_ns; }
net_socket* net_interface_tcp::connect(const char* c_host, int port) { assert(c_host); assert(port > 0); m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == INVALID_SOCKET) { fprintf(stderr, "can't open listen socket\n"); return NULL; } // Set the address. SOCKADDR_IN saddr; saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; m_port_number = get_proxy_port() > 0 ? get_proxy_port() : port; saddr.sin_port = htons(m_port_number); hostent* he; const char* host = get_proxy_port() > 0 ? get_proxy() : c_host; if (host[0] >= '0' && host[0] <= '9') // absolue address ? { Uint32 addr = inet_addr(host); he = gethostbyaddr((char *) &addr, 4, AF_INET); } else { he = gethostbyname(host); } if (he == NULL) { fprintf(stderr, "can't find '%s'\n", host); closesocket(m_socket); m_socket = INVALID_SOCKET; return NULL; } // get server address memcpy(&saddr.sin_addr, he->h_addr, he->h_length); // printf("The IP address is '%d.%d.%d.%d'\n", // saddr.sin_addr.S_un.S_un_b.s_b1, // saddr.sin_addr.S_un.S_un_b.s_b2, // saddr.sin_addr.S_un.S_un_b.s_b3, // saddr.sin_addr.S_un.S_un_b.s_b4); int rc = ::connect(m_socket, (struct sockaddr*) &saddr, sizeof(struct sockaddr)); if (rc != 0) { fprintf(stderr, "can't connect to '%s', port %d\n", host, m_port_number); closesocket(m_socket); m_socket = INVALID_SOCKET; return NULL; } // Set non-blocking mode for the socket, so that // accept() doesn't block if there's no pending // connection. set_nonblock(); return new net_socket_tcp(m_socket); }