Compare commits

..

6 Commits

4 changed files with 62 additions and 34 deletions

View File

@ -28,13 +28,16 @@ class http_request {
//dolazni //dolazni
http_request(const string _raw); http_request(const string _raw);
void putheader(const string _key, const string _value); //bool validate();
void setheaders(const map<string, string> _headers); void header(const string _key, const string _value);
void header(const map<string, string> _headers);
private:
void parse(); void parse();
void mold(); void mold();
}; };
static string get_protocol(const string _protocol);
class http_response { class http_response {
public: public:
@ -46,14 +49,15 @@ class http_response {
//konstruktor odgovora //konstruktor odgovora
// odlazni // 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 // dolaznih
http_response(const string _raw); http_response(const string _raw);
//bool validate(); //bool validate();
// void putheader(const string _key, const string _value); void header(const string _key, const string _value);
// void putheaders(const map<string, string> _headers); void header(const map<string, string> _headers);
private:
void mold(); // za slanje void mold(); // za slanje
void parse(); // čitaj http void parse(); // čitaj http
}; };

View File

@ -3,16 +3,22 @@
http_request::http_request(const http_method _method, const string _url, const string _body, const string _protocol) { http_request::http_request(const http_method _method, const string _url, const string _body, const string _protocol) {
method = http_method_to_str(_method); method = http_method_to_str(_method);
url = _url; url = _url;
body = _body; protocol = get_protocol(_protocol);
protocol = set_protcol(_protocol); if (!_body.empty()) {
body = _body;
this->header("Content-Length", to_string(body.length()));
}
mold(); mold();
} }
http_request::http_request(const api *_api, const string _protocol) { http_request::http_request(const api *_api, const string _protocol) {
method = _api->method; method = _api->method;
url = _api->url; url = _api->url;
body = _api->body; protocol = get_protocol(_protocol);
protocol = set_protcol(_protocol); if (!_api->body.empty()) {
body = _api->body;
this->header("Content-Length", to_string(body.length()));
}
mold(); mold();
} }
@ -48,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; headers[_key] = _value;
mold(); mold();
} }
void http_request::setheaders(const map<string, string> _headers) { void http_request::header(const map<string, string> _headers) {
headers = _headers; headers = _headers;
mold(); mold();
} }
@ -65,7 +71,7 @@ void http_request::setheaders(const map<string, string> _headers) {
void http_request::mold() { void http_request::mold() {
raw = method.empty() ? "GET" : method; raw = method.empty() ? "GET" : method;
raw += " " + url + " HTTP/1.1\r\n"; raw += " " + url + " " + protocol + "\r\n";
if (!headers.empty()) { if (!headers.empty()) {
for (auto i : headers) { for (auto i : headers) {
@ -78,26 +84,46 @@ void http_request::mold() {
http_response::http_response(const http_response_code _status, const string _body, const string _protocol) { http_response::http_response(const http_response_code _status, const string _body, const string _protocol) {
status = to_string(_status) + " " + http_response_code_txt(_status); status = to_string(_status) + " " + http_response_code_txt(_status);
body = _body; if (!_body.empty()) {
protocol = set_protcol(_protocol); body = _body;
this->header("Content-Length", to_string(body.length()));
}
protocol = get_protocol(_protocol);
mold(); 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";
}
return protocol;
}
http_response::http_response(const string _raw) { http_response::http_response(const string _raw) {
raw = _raw; raw = _raw;
parse(); parse();
} }
/** void http_response::header(const string _key, const string _value) {
* headers[_key] = _value;
*/ mold();
}
void http_response::header(const map<string, string> _headers) {
headers = _headers;
mold();
}
void http_response::mold() { void http_response::mold() {
raw = protocol + " " + status + "\r\n"; //"HTTP/1.1 200 OK\r\n"; // implementirati status raw = protocol + " " + status + "\r\n"; //"HTTP/1.1 200 OK\r\n"; // implementirati status
if (!headers.empty()) { if (!headers.empty()) {
raw += '?';
for (auto i : headers) { for (auto i : headers) {
raw += i.first + ": " + i.second + "\r\n"; raw += i.first + ": " + i.second + "\r\n";
} }

View File

@ -8,26 +8,28 @@ int main() {
try { try {
api uf(GET, "delete", {make_pair("id", "4")}, "bay"); api uf(GET, "/delete", {make_pair("id", "4")}, "bay");
http_request myreq(&uf); http_request myreq(&uf);
myreq.putheader("Content-type", "text/plain"); 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 << myreq.protocol << endl;
cout << myres.protocol << endl; // cout << myreq.status << endl;
cout << myres.status << endl; for(auto i : myreq.headers)
// cout << myres.headers << endl; cout << i.first << " " << i.second << endl;
cout << myres.body << endl; cout << myreq.body << endl;
http_response myres2(OK, "nnotauth", "1.0" ); http_response myres2(OK, "nnotauth", "1.0" );
cout << myres2.protocol << endl; cout << myres2.protocol << endl;
cout << myres2.status << endl; cout << myres2.status << endl;
// cout << myres2.headers << endl; cout << myres2.headers["Content-Length"] << endl;
cout << myres2.body << endl; cout << myres2.body << endl;
} catch (const string err) {
cout << err << endl;
}
// http myHttp(&myApi, "GET /fghfhf HTTP/1.1\r\nBaba"); // http myHttp(&myApi, "GET /fghfhf HTTP/1.1\r\nBaba");
@ -49,19 +51,15 @@ int main() {
// myres.get("HTTP/1.1 200 OK\r\n\r\nnotauth"); // myres.get("HTTP/1.1 200 OK\r\n\r\nnotauth");
// myres.get("HTTP/1.1 200 OK"); // myres.get("HTTP/1.1 200 OK");
// myres.header("Content-type", "text/plain");
// api uf(&myApi, myres); // api uf(&myApi, myres);
// cout << uf.method << " " << uf.path << " " << uf.body << endl; // cout << uf.method << " " << uf.path << " " << uf.body << endl;// << myres.raw;
// cout << myres.method << " " << myres.url << " " << myres.body << endl << myres.raw << endl; // cout << myres.method << " " << myres.url << " " << myres.body << endl;// << myres.raw;
// for(auto i : uf.params) // for(auto i : uf.params)
// cout << i.first << " " << i.second << endl; // cout << i.first << " " << i.second << endl;
} catch (string err) {
cout << err << endl;
}
} }

Binary file not shown.