headers are now always lower case
This commit is contained in:
parent
0fbc5bad52
commit
64739c39d8
2 changed files with 50 additions and 47 deletions
|
|
@ -47,36 +47,39 @@ ClientHTTP::ClientHTTP(std::string host, std::uint16_t port): ClientHTTP(host.c_
|
|||
|
||||
HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
||||
client.Send(request, length);
|
||||
std::vector<char> buffer = client.RecieveSync(1024);
|
||||
std::vector<char> buffer;
|
||||
HTTPResponse response;
|
||||
std::uint32_t i = 0;
|
||||
std::uint32_t statusStart = 0;
|
||||
for(; i < 1024; i++) {
|
||||
while(true) {
|
||||
buffer = client.RecieveSync();
|
||||
for(; i < buffer.size(); i++) {
|
||||
if(buffer[i] == ' ') {
|
||||
statusStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(; i < 1024; i++) {
|
||||
for(; i < buffer.size(); i++) {
|
||||
if(buffer[i] == '\r') {
|
||||
response.status.assign(buffer.data()+statusStart+1, i-statusStart-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
i+=2;
|
||||
while(i < 1024) {
|
||||
while(i < buffer.size()) {
|
||||
std::uint32_t headerStart = i;
|
||||
std::string headerName;
|
||||
for(; i < 1024; i++) {
|
||||
for(; i < buffer.size(); i++) {
|
||||
if(buffer[i] == ':') {
|
||||
headerName.assign(buffer.data()+headerStart, i-headerStart);
|
||||
std::transform(headerName.begin(), headerName.end(), headerName.begin(), [](unsigned char c){ return std::tolower(c); });
|
||||
i+=2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
headerStart = i;
|
||||
std::string headerValue;
|
||||
for(; i < 1024; i++) {
|
||||
for(; i < buffer.size(); i++) {
|
||||
if(buffer[i] == '\r' && buffer[i+1] == '\n') {
|
||||
headerValue.assign(buffer.data()+headerStart, i-headerStart);
|
||||
response.headers.insert({headerName, headerValue});
|
||||
|
|
@ -89,9 +92,12 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
i = 0;
|
||||
}
|
||||
headersComplete:;
|
||||
i+=4;
|
||||
std::unordered_map<std::string, std::string>::iterator it = response.headers.find("Content-Length");
|
||||
std::unordered_map<std::string, std::string>::iterator it = response.headers.find("content-length");
|
||||
if(it != response.headers.end())
|
||||
{
|
||||
const int lenght = std::stoi(it->second);
|
||||
|
|
@ -109,7 +115,7 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
std::unordered_map<std::string, std::string>::iterator it = response.headers.find("Transfer-Encoding");
|
||||
std::unordered_map<std::string, std::string>::iterator it = response.headers.find("transfer-encoding");
|
||||
if(it != response.headers.end() && it->second == "chunked") {
|
||||
while(i < buffer.size()){
|
||||
std::string lenght;
|
||||
|
|
@ -121,7 +127,6 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
|||
}
|
||||
}
|
||||
i+=2;
|
||||
std::cout << lenght << "bro" << std::endl;
|
||||
int lenghtInt = stoi(lenght, 0, 8);
|
||||
if(lenghtInt != 0){
|
||||
int oldSize = response.body.size();
|
||||
|
|
@ -138,7 +143,6 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
|||
goto bodyFinished;
|
||||
}
|
||||
}
|
||||
std::cout << response.body << std::endl;
|
||||
while(true) {
|
||||
std::vector<char> bodyBuffer = client.RecieveSync();
|
||||
int i2 = 0;
|
||||
|
|
@ -153,7 +157,6 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const {
|
|||
}
|
||||
i2+=2;
|
||||
int lenghtInt = stoi(lenght, 0, 8);
|
||||
std::cout <<lenghtInt << std::endl;
|
||||
if(lenghtInt != 0){
|
||||
int oldSize = response.body.size();
|
||||
response.body.resize(oldSize+lenghtInt, 0);
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ namespace Crafter {
|
|||
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nHost: {}\r\n\r\n", method, route, host);
|
||||
}
|
||||
|
||||
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::unordered_map<std::string, std::string> headers, std::string host) {
|
||||
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::unordered_map<std::string, std::string> headers) {
|
||||
std::string headersString;
|
||||
for (auto const& [key, val] : headers) {
|
||||
headersString+=std::format("{}: {}\r\n", key, val);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue