I am trying to make an OOP abstraction for a very simple HTTP client. I have also created a simple abstraction for a (GET only for now) HTTP request.
I am using winsock2
sockets for Windows and examples from the internet.
This is the request class:
httpRequest.h
#ifndef httpRequest_H
#define httpRequest_H
#include <string>
#include <vector>
class httpRequest
{
std::string method;
std::string httpVer;
std::string host;
std::string fullHost;
std::string uri;
std::vector<std::string> headers;
std::string parsedHeaders;
std::string fullPath;
std::string fullRequest;
void initialize(std::string methodName, std::string hostName, std::string uriValue);
std::string parseHeaders();
public:
httpRequest();
void setMethod(std::string methodName);
void addHeader(std::string httpHeader);
void setHost(std::string hostName);
void setUri(std::string uriValue);
void buildRequest();
int getRequestLength();
std::string getHost();
std::string toString();
};
#endif
httpRequest.cpp
#include "httpRequest.h"
#include <string>
#include <iostream>
httpRequest::httpRequest()
{
this->initialize("GET", "127.0.0.1", "/");
//this->fullRequest = "You need to add host URI and/or some headers";
}
void httpRequest::initialize(std::string methodName, std::string hostName, std::string uriValue)
{
this->method = methodName;
this->httpVer = "HTTP/1.1";
this->host = hostName;
this->uri = " " + uriValue + " ";
}
std::string httpRequest::parseHeaders()
{
for(int i = 0; i < this->headers.size(); i++)
{
this->parsedHeaders += this->headers.at(i);
}
return this->parsedHeaders;
}
void httpRequest::setMethod(std::string methodName)
{
this->method = methodName;
}
void httpRequest::setHost(std::string hostName)
{
this->fullHost = "HOST: " + hostName;
this->host = hostName;
}
void httpRequest::setUri(std::string uriValue)
{
this->uri = " " + uriValue + " ";
}
void httpRequest::addHeader(std::string httpHeader)
{
this->headers.push_back(httpHeader + "\r\n");
}
void httpRequest::buildRequest()
{
this->fullRequest = this->method + this->uri + this->httpVer + "\r\n";
this->fullRequest = this->fullRequest + this->fullHost + "\r\n";
this->fullRequest = this->fullRequest + this->parseHeaders() + "\r\n";
this->fullRequest = this->fullRequest + "\r\n";
}
std::string httpRequest::getHost()
{
return this->host;
}
std::string httpRequest::toString()
{
return this->fullRequest;
}
int httpRequest::getRequestLength()
{
return this->fullRequest.length();
}
The HTTP client:
httpClient.h
#ifndef httpClient_H
#define httpClient_H
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <winsock.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include "httpRequest.h"
class httpClient
{
WSADATA wsaData;
SOCKET connectSocket;
std::string ip;
std::string resp;
struct addrinfo *result, *ptr, hints;
void connectTo(std::string ip);
std::string receive(httpRequest request);
public:
httpClient();
void sendRequest(httpRequest request);
std::string getResponse();
};
#endif
httpClient.cpp
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <winsock.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include "httpClient.h"
#include "httpRequest.h"
#include <string>
#pragma comment(lib, "ws2_32.lib")
#pragma GCC diagnostic ignored "-Wwrite-strings"
#define DEFAULT_PORT "80"
httpClient::httpClient()
{
int wsResult = 0;
wsResult = WSAStartup(MAKEWORD(2,2), &this->wsaData);
if (wsResult != 0) {
std::cout<<"WSAStartup failed with error: "<<wsResult<<std::endl;
}
ZeroMemory( &this->hints, sizeof(this->hints) );
this->hints.ai_family = AF_UNSPEC;
this->hints.ai_socktype = SOCK_STREAM;
this->hints.ai_protocol = IPPROTO_TCP;
}
void httpClient::connectTo(std::string ip)
{
int wsResult = 0;
wsResult = getaddrinfo(ip.c_str(), DEFAULT_PORT, &this->hints, &result);
if ( wsResult != 0 ) {
std::cout<<"getaddrinfo failed with error: "<<wsResult<<std::endl;
WSACleanup();
}
for(this->ptr=this->result; this->ptr != NULL ;this->ptr=this->ptr->ai_next) {
// Create a SOCKET
this->connectSocket = socket(this->ptr->ai_family, this->ptr->ai_socktype,
this->ptr->ai_protocol);
if (this->connectSocket == INVALID_SOCKET) {
std::cout<<"Socket failed with error: "<<WSAGetLastError();
WSACleanup();
}
// Connect to server.
wsResult = connect( this->connectSocket, this->ptr->ai_addr, (int)this->ptr->ai_addrlen);
if (wsResult == SOCKET_ERROR) {
closesocket(this->connectSocket);
this->connectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (this->connectSocket == INVALID_SOCKET) {
std::cout<<"Unable to connect to server!\n";
WSACleanup();
}
}
std::string httpClient::receive(httpRequest request)
{
int iResult = 0, requestLength = 0, iSent = 0;
requestLength = request.getRequestLength();
char buffer[10054];
do {
iResult = send(this->connectSocket, request.toString().c_str(), requestLength - iSent, 0 );
iSent = iSent + iResult;
} while(iSent < requestLength);
iResult = recv(this->connectSocket, buffer, 10054, 0);
if(iResult > 0)
{
//std::cout<<buffer<<std::endl;
}
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(this->connectSocket);
WSACleanup();
std::string s = buffer;
return s;
}
void httpClient::sendRequest(httpRequest request)
{
this->connectTo(request.getHost());
this->resp = this->receive(request);
}
std::string httpClient::getResponse()
{
return this->resp;
}
How this works is :
//You create the request
httpRequest r;
//Set the host,uri and headers
r.setHost("www.learncpp.com");
r.setUri("/");
r.addHeader("Connection: close");
//Build the request
r.buildRequest();
//Create the client
httpClient *cl = new httpClient()
//Send the request
cl->sendRequest(r);
//Get the Response
cout<<cl->getResponse()<<endl;
Now I want to know, since I am an absolute C++ beginner (though I do have experience in web programming), what I could improve in terms of OOP design and client architecture.
r.setHost().setUri().addHeader().buildRequest()
methods so that you don't have to keep typing "r
" over and over. \$\endgroup\$