-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncHttp401Handler.cxx
More file actions
123 lines (94 loc) · 3.44 KB
/
syncHttp401Handler.cxx
File metadata and controls
123 lines (94 loc) · 3.44 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "syncHttp401Handler.hxx"
#include "echoRequest.hxx"
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/istream_iterator.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include "base64.hxx"
namespace {
namespace iter = boost::archive::iterators;
void dumpString(std::string caption, std::string value)
{
std::cout << caption << ":\n";
std::cout << "\t";
for (auto ch: value) {
std::cout << ch << "(" << int(ch) << ") ";
}
std::cout << "\n";
}
SyncHttp401Handler::SyncHttpServer::request::header_type findHeaderByName (
std::string targetName,
const SyncHttp401Handler::SyncHttpServer::request& request)
{
for (auto header: request.headers) {
if (header.name == targetName) {
return header;
}
}
throw std::range_error ("Header named " + targetName + " not found");
}
void printAuthorizationHeaderParts (
const SyncHttp401Handler::SyncHttpServer::request& request)
{
try
{
auto header = findHeaderByName ("Authorization", request);
std::cout << "Header name: " << header.name << "\n";
std::stringstream headerValue {header.value};
std::string authorizationType;
std::string base64data;
headerValue >> authorizationType >> base64data;
std::string stripped64data = base64data.substr(0, base64data.find("="));
std::cout << "\t AuthorizationType: " << authorizationType << "\n"
<< "\t data (base64): " << base64data << "\n";
std::cout << "base64 length: " << base64data.size() << "\n\n";
dumpString ("original", base64data);
dumpString ("stripped", stripped64data);
base64decodingIterator iDecodedStart { base64data.begin() };
base64decodingIterator iDecodedEnd { base64data.begin() + base64data.find("=") };
std::string decodedData { iDecodedStart, iDecodedEnd };
dumpString ("decoded(1)", decodedData);
base64encodingIterator iEncodedStart { decodedData.begin() };
base64encodingIterator iEncodedEnd { decodedData.end() };
std::string reencodedData = { iEncodedStart, iEncodedEnd };
reencodedData += "=";
dumpString ("re-encoded", reencodedData);
dumpString("Decoded twice",
std::string (base64decodingIterator (reencodedData.begin()),
base64decodingIterator (reencodedData.begin() + reencodedData.find("="))));
}
catch (const std::range_error& ex)
{
// Ignore,
// No Authorization-header found so do not print anything.
}
}
}
void SyncHttp401Handler::operator() (
const SyncHttpServer::request& request,
SyncHttpServer::response& response)
{
using Header = SyncHttpServer::response_header;
std::cout << "receveid request:\n";
std::cout << echoRequest(Synchronicity::SYNCHRONE, request);
std::cout << "\n<\\end request>\n\n";
printAuthorizationHeaderParts(request);
response = SyncHttpServer::response::stock_reply (
SyncHttpServer::response::unauthorized,
"<html><head><title>Demo: unathorized</title></head>\n"
"<body><h1>Demo: unathorized</h1>\n"
"This demo server always responds with 401 :: unauthorized\n"
"</body></html>\n\n");
response.headers.push_back(Header {
"WWW-Authenticate",
"Basic realm=\"test\""});
}
void SyncHttp401Handler::log(const SyncHttpServer::string_type& info)
{
std::cerr << "ERROR " << info << '\n';
}
/* vim:set tabstop=4 shiftwidth=4 fo=cqwan autoindent : */
/* makeprg=make\ -C\ ~/tmp/build-try-https */