int isValidPort(const char * str) { if(str == NULL) return FALSE; if(!isNum(str) && !isValidPortRange(str)) return FALSE; return TRUE; }
int HttpSocket::parseURL(CString strurl) { int index = strurl.Find("/",7); /** * Check if resource to be retrieved from * the host is specified. Seperate the host * or authority from the resource and http * port specified if any. * e.g. http://www.abc.com:8080/ * { * Scheme : http * Authority or host : www.abc.com * HTTP port on host : 8080 * Resource : / * } */ if(index != -1){ /** * If the scheme http is already specified * in the HTTP URI,then discard it and * seperate the host and the specified * resource. */ if(strurl.Find("http://",0)==-1) host = strurl.Mid(0, index); else host = strurl.Mid(7, index-7); if(host.GetLength() != 0) resource = strurl.Mid(index); else { host = strurl.Mid(7); resource = "/"; } } else { if(strurl.Find("http://",0)==-1) { /** Only HTTP protocol scheme in the * specified URI will be accepted */ if(strurl.Find("://",0) != -1) { sessionErrorCode = NonHTTPScheme; return NonHTTPScheme; } host = strurl.Mid(0); } else host = strurl.Mid(7); //The default resource is / resource = "/"; } // If Port is present in the URI, seperate // it from the host if(host.Find(":") != -1) { setPortSpecified(true); index = host.Find(":"); CString strPortno = host.Mid(index+1); portNo = atoi((LPCSTR)strPortno); if(isValidPortRange(portNo)==false) { sessionErrorCode = InvalidHTTPPortNo; return sessionErrorCode; } host = host.Mid(0, index); } return 0; }
/** * Formulate the Http Request and send it. * Read the response for meta-information and parse the Server Configuration information. * Params : URL , port no and the method (HEAD or GET) */ int HttpSocket::sendHttpRequest(HttpParams *hq) { CString strurl, useragent, httpRequest; HTTP_METHOD method; CONN_TYPE connType; int port=0, proxyport=0; char strPortno[6] = {0}; strurl = hq->getURI(); method = hq->getHttpMethod(); useragent = hq->getUserAgent(); connType = hq->getConnType(); port = hq->getHttpPort(); if((sessionErrorCode = parseURL(strurl)) != 0) return sessionErrorCode; /** If the port is specified in the URI * that gets the priority over what is * specified in the HTTP port edit box. */ if(isPortSpecified()==true) port = portNo; else portNo = port; /** If the port is not specified by the * user in the HTTP port edit control * then use the default port 80 */ if(isValidPortRange(port)==false) { sessionErrorCode = InvalidHTTPPortNo; return sessionErrorCode; } /** Construct the HTTP request to be sent to the * HTTP Server. */ if(method == HEAD) httpRequest = "HEAD "; else httpRequest = "GET "; /** If the HTTP connection is through the proxy, the * absolute URI of the resource requested needs to * be send as a parmeter of the HTTP method in HTTP * header. */ if(connType == THRU_PROXY) { /** The resource with http port needs to be given * as specified in the requested URI. */ if(isPortSpecified()==true) { itoa(port,strPortno,10); httpRequest = httpRequest + scheme + host + ":" + CString(strPortno) + resource + " HTTP/1.1\r\n"; } else httpRequest = httpRequest + scheme + host + resource + " HTTP/1.1\r\n"; } else httpRequest = httpRequest + resource + " HTTP/1.1\r\n"; httpRequest = httpRequest + "Host: " + host + "\r\n"; httpRequest = httpRequest + "User-Agent: " + useragent + "\r\n"; httpRequest = httpRequest + "Accept: */*\r\n"; httpRequest = httpRequest + "Accept-Language: en-us,en;q=0.5\r\n"; httpRequest = httpRequest + "Connection: close\r\n" + "\r\n"; struct sockaddr_in hostaddr; /** * If connection is through HTTP Proxy then * TCP connection should be established with * it and then then HTTP requset needs to be * sent on the connection. */ if(connType == THRU_PROXY) { if(getHostAddr(hq->getProxyIP(),hostaddr) != 0) { return sessionErrorCode; } hostaddr.sin_port = htons(hq->getProxyPort()); } else { /** Connect to the host HTTP Server on the * specified port no */ if(getHostAddr((LPCSTR)host,hostaddr) != 0) { return sessionErrorCode; } hostaddr.sin_port = htons(portNo); } if(connect(sock, (struct sockaddr *)&hostaddr, sizeof(hostaddr)) == SOCKET_ERROR) { if(connType == THRU_PROXY) sessionErrorCode = HTTPProxyConnErr; else sessionErrorCode = HTTPSrvrConnErr; return sessionErrorCode; } // Send an HTTP request to the host at the specified port no int writtenBytes = send(sock, (LPCSTR) httpRequest, httpRequest.GetLength(),0); int readBytes = 0; memset(strHttpResponse,0,RESLEN); // Clear the receive buffer if(writtenBytes <=0) { sessionErrorCode = ZeroBytesSent; return sessionErrorCode; } if(IsSocketReadible(sock)) { readBytes = recvfrom(sock, strHttpResponse, RESLEN-1, 0, 0, 0); if(readBytes > 0) { strHttpResponse[readBytes] = '\0'; } } return 0; }