diff --git a/lib/http.hpp b/lib/http.hpp index 1c4454e..84417e3 100644 --- a/lib/http.hpp +++ b/lib/http.hpp @@ -15,6 +15,7 @@ class api; class http_request { public: + string protocol; string method; string url; map headers; @@ -23,19 +24,21 @@ class http_request { // konstruktori zahtjeva // odlazni - http_request(const http_method _method, const string _url, const string _body = ""); - http_request(const api *_api); + http_request(const http_method _method, const string _url, const string _body = "", const string _protocol = "1.1"); + http_request(const api *_api, const string _protocol = "1.1"); //dolazni http_request(const string _raw); //bool validate(); - void putheader(const string _key, const string _value); - void setheaders(const map _headers); + void header(const string _key, const string _value); + void header(const map _headers); + private: void parse(); void mold(); }; +static string get_protocol(const string _protocol); class http_response { public: @@ -47,7 +50,7 @@ class http_response { //konstruktor odgovora // odlazni - http_response(const http_response_code _status, const string _body, const string _protocol = "1.1"); + http_response(const http_response_code _status, const string _body = "", const string _protocol = "1.1"); // dolaznih http_response(const string _raw); @@ -55,6 +58,7 @@ class http_response { // void putheader(const string _key, const string _value); // void putheaders(const map _headers); + private: void mold(); // za slanje void parse(); // čitaj http }; diff --git a/src/http.cpp b/src/http.cpp index 8844a68..9d371d4 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1,16 +1,24 @@ #include "../lib/http.hpp" -http_request::http_request(const http_method _method, const string _url, const string _body) { +http_request::http_request(const http_method _method, const string _url, const string _body, const string _protocol) { method = http_method_to_str(_method); url = _url; - body = _body; + protocol = get_protocol(_protocol); + if (!_body.empty()) { + body = _body; + this->header("Content-Length", to_string(body.length())); + } mold(); } -http_request::http_request(const api *_api) { +http_request::http_request(const api *_api, const string _protocol) { method = _api->method; url = _api->url; - body = _api->body; + protocol = get_protocol(_protocol); + if (!_api->body.empty()) { + body = _api->body; + this->header("Content-Length", to_string(body.length())); + } mold(); } @@ -46,12 +54,12 @@ void http_request::parse() { } -void http_request::putheader(const string _key, const string _value) { +void http_request::header(const string _key, const string _value) { headers[_key] = _value; mold(); } -void http_request::setheaders(const map _headers) { +void http_request::header(const map _headers) { headers = _headers; mold(); } @@ -63,7 +71,7 @@ void http_request::setheaders(const map _headers) { void http_request::mold() { raw = method.empty() ? "GET" : method; - raw += " " + url + " HTTP/1.1\r\n"; + raw += " " + url + " " + protocol + "\r\n"; if (!headers.empty()) { for (auto i : headers) { @@ -76,14 +84,25 @@ void http_request::mold() { http_response::http_response(const http_response_code _status, const string _body, const string _protocol) { status = to_string(_status) + " " + http_response_code_txt(_status); - body = _body; + if (!_body.empty()) { + body = _body; + // kad merge s header-response branch možeš omogućit ovu liniju + // this->header("Content-Length", to_string(body.length())); + } + protocol = get_protocol(_protocol); + mold(); +} + + +static string get_protocol(const string _protocol) { + string protocol; if (_protocol == "1.0" || _protocol == "1.1" || _protocol == "2.0") { protocol = "HTTP/" + _protocol; } else { protocol = "HTTP/1.1"; } - mold(); + return protocol; } diff --git a/test/test.cpp b/test/test.cpp index eb6a9fa..7031963 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -14,23 +14,23 @@ int main() { try { - api uf(&myApi, GET, "delete", {make_pair("id", "4")}, "bay"); - http_request myreq(&uf); - myreq.putheader("Content-type", "text/plain"); + // api uf(&myApi, GET, "/delete", {make_pair("id", "4")}, "bay"); + // http_request myreq(&uf); + // myreq.header("Content-type", "text/plain"); - cout << myreq.raw << endl; + // cout << myreq.raw << endl; - http_response myres("HTTP/1.1 200 OK\r\n\r\nnotauth"); - cout << myres.protocol << endl; - cout << myres.status << endl; - // cout << myres.headers << endl; - cout << myres.body << endl; + // cout << myreq.protocol << endl; + // // cout << myreq.status << endl; + // for(auto i : myreq.headers) + // cout << i.first << " " << i.second << endl; + // cout << myreq.body << endl; http_response myres2(OK, "nnotauth" ); cout << myres2.protocol << endl; cout << myres2.status << endl; - // cout << myres2.headers << endl; + cout << myres2.headers["Content-Length"] << endl; cout << myres2.body << endl; diff --git a/test/test.o b/test/test.o index d321575..e93ec13 100755 Binary files a/test/test.o and b/test/test.o differ