Continuing client/server http

unix_test
anulax1225 ago%!(EXTRA string=1 year)
parent 0884bcaf1c
commit 644fada095
  1. 9
      sandbox/client/client.cpp
  2. 4
      sandbox/commun.h
  3. 68
      sandbox/server/server.cpp

@ -15,15 +15,18 @@ void http_client()
log(dns_lookup("edus2.rpn.ch", IpVersion::IPv4)[0])
IpAddress ip("127.0.0.1");
Socket sock(ip, 80, IpProtocol::TCP);
Socket sock(ip, PORT, IpProtocol::TCP);
if(!sock.conn()) {
if(!sock.conn())
{
perror("Couldn't connect to the end point.");
exit(1);
}
Packet packet;
std::string str("GET / HTTP/1.1\r\n\r\n");
std::string str = "GET / HTTP/1.1\r\n"
"Host: 127.0.0.1:10001\r\n\r\n"
"Body jylkdkjlkjlkjlkjlkj";
packet.push<char>(str.c_str(), str.length());
sock.send(packet.payload);

@ -5,4 +5,6 @@
#include <string>
#include <bakanet.h>
#define log(str) std::cout << str << "\n";
#define log(str) std::cout << str << "\n";
#define PORT 10001

@ -2,8 +2,20 @@
using namespace Bk::Net;
enum class HttpMethod
{
NONE = 0,
GET = 1,
POST = 2,
PUT = 3,
DELETE = 4,
};
void http_server();
std::string http_handle(Socket& sock, Connection conn);
std::string http_handler(Socket& sock, Connection conn);
void http_parser(std::string req);
HttpMethod resolve_methode(std::string method);
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter);
int main()
{
@ -14,22 +26,23 @@ int main()
void http_server()
{
IpAddress ip("127.0.0.1");
Socket sock(ip, 80, IpProtocol::TCP);
Socket sock(ip, PORT, IpProtocol::TCP);
bool running = sock.init() && sock.start(5);
while (running)
{
Connection conn = sock.ack();
if (conn >= 0)
{
log(http_handle(sock, conn));
std::string http_request(http_handler(sock, conn));
http_parser(http_request);
}
}
}
std::string http_handle(Socket& sock, Connection conn)
std::string http_handler(Socket& sock, Connection conn)
{
Packet req;
bool reading = true;
while(reading)
{
@ -45,4 +58,49 @@ std::string http_handle(Socket& sock, Connection conn)
if (req_size) return std::string(req_test.release(), req_size);
return "";
}
void http_parser(std::string req)
{
auto lines = string_split(req, "\r\n");
for (auto line : *lines)
{
auto param = string_split(line, ":");
}
}
HttpMethod resolve_methode(std::string method)
{
if (method == "GET") return HttpMethod::GET;
else if (method == "POST") return HttpMethod::POST;
else if (method == "PUT") return HttpMethod::PUT;
else if (method == "DELETE") return HttpMethod::DELETE;
else return HttpMethod::NONE;
}
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter)
{
std::unique_ptr<std::vector<std::string>> splits(new std::vector<std::string>(0));
size_t pos = 0;
while ((pos = s.find(delimiter)) != std::string::npos) {
splits->push_back(s.substr(0, pos));
s.erase(0, pos + delimiter.length());
}
splits->push_back(s);
return splits;
}
std::string string_trim(const std::string& str, const std::string& whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
Loading…
Cancel
Save