forked from fnc12/embeddedRest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.hpp
88 lines (76 loc) · 2.43 KB
/
Response.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Response.hpp
// urldTest
//
// Created by John on 12.12.15.
// Copyright © 2015 Outlaw Studio. All rights reserved.
//
#pragma once
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
class Response{
public:
struct IncorrectStartLineException{
const std::string startLine;
};
protected:
int _statusCode;
std::string _statusDescription;
std::vector<std::string> _headers;
std::string _body;
std::string _httpVersion;
static void parseStartLine(const std::string &startLine,
decltype(_httpVersion) &httpVersion,
decltype(_statusCode) &statusCode,
decltype(_statusDescription) &statusDescription) throw(IncorrectStartLineException)
{
std::stringstream stream(startLine);
if(stream.good()){
stream>>httpVersion;
if(stream.good()){
stream>>statusCode;
if(stream.good()){
while(stream.good()){
std::string word;
stream>>word;
statusDescription+=word+" ";
}
}else{
throw IncorrectStartLineException{startLine};
}
}else{
throw IncorrectStartLineException{startLine};
}
}else{
throw IncorrectStartLineException{startLine};
}
}
public:
Response(const std::string &startLine,decltype(_headers)&&headers,decltype(_body)&&body) throw(IncorrectStartLineException):
_headers(std::move(headers)),
_body(std::move(body))
{
parseStartLine(startLine, _httpVersion, _statusCode, _statusDescription);
}
Response(decltype(_statusCode)statusCode_,decltype(_statusDescription)&&statusDescription_,decltype(_body)&&body_):
_statusCode(statusCode_),
_statusDescription(std::move(statusDescription_)),
_body(std::move(body_)){}
const decltype(_body)& body() const{
return _body;
}
const decltype(_headers)& headers() const{
return _headers;
}
const decltype(_httpVersion)& httpVersion() const{
return _httpVersion;
}
decltype(_statusCode) statusCode() const{
return _statusCode;
}
const decltype(_statusDescription)& statusDescription() const{
return _statusDescription;
}
};