-
Notifications
You must be signed in to change notification settings - Fork 48
/
HttpUtil.cc
114 lines (93 loc) · 2.77 KB
/
HttpUtil.cc
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
#include <string>
#include <vector>
#include <cstdint>
#include <evhtp.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <univalue.h>
#include "HttpUtil.h"
#include "Util.h"
using namespace std;
bool query_int64_range(const evhtp_request_t *req,
const char *query_key,
int64_t& vOut,
int64_t vMin, int64_t vMax, int64_t vDefault)
{
assert(req && query_key);
vOut = vDefault;
// no query string; return default
if (!req->uri || !req->uri->query)
return true;
// query key not present; return default
const char *value = evhtp_kv_find (req->uri->query, query_key);
if (!value)
return true;
// invalid query value; return error
errno = 0;
int64_t val = (int64_t) strtoll(value, NULL, 10);
if (errno != 0)
return false;
// valid value within range; return value
if ((val >= vMin) && (val <= vMax)) {
vOut = val;
return true;
}
// invalid value outside range; return error
return false;
}
int64_t get_content_length (const evhtp_request_t *req)
{
assert(req != NULL);
const char *content_len_str = evhtp_kv_find (req->headers_in, "Content-Length");
if (!content_len_str) {
return -1;
}
return strtoll (content_len_str, NULL, 10);
}
std::string httpDateHdr(time_t t)
{
return formatTime("%a, %d %b %Y %H:%M:%S GMT", t);
}
void httpJsonReply(evhtp_request_t *req, const UniValue& jval)
{
string body = jval.write(2) + "\n";
evhtp_headers_add_header(req->headers_out,
evhtp_header_new("Content-Type", "application/json; charset=utf-8", 0, 0));
evbuffer_add(req->buffer_out, body.c_str(), body.size());
evhtp_send_reply(req, EVHTP_RES_OK);
}
void build_auth_hdr(evhtp_request_t *req,
const std::string& auth_user,
const std::string& auth_secret,
std::string& auth_hdr)
{
evhtp_headers_t *hdrs = req->headers_in;
// build canonical pseudo-header
// phdr header: static auth method, auth user id
string phdr("cscpp1-sha256\n");
phdr += auth_user + "\n";
// absorb Host, X-Unixtime, ETag
// It is assumed ETag will guaranteed message contents
// ETag is signed here, checked elsewhere.
const char *hdr = evhtp_kv_find(hdrs, "Host");
if (hdr && *hdr)
phdr += string(hdr) + "\n";
hdr = evhtp_kv_find(hdrs, "X-Unixtime");
if (hdr && *hdr)
phdr += string(hdr) + "\n";
hdr = evhtp_kv_find(hdrs, "ETag");
if (hdr && *hdr)
phdr += string(hdr) + "\n";
// create HMAC signature
vector<unsigned char> md(SHA256_DIGEST_LENGTH);
HMAC(EVP_sha256(),
&auth_secret[0], auth_secret.size(),
(const unsigned char *) phdr.c_str(), phdr.size(),
&md[0], NULL);
string signature(HexStr(md));
// format: bbnode1-sha256 $username $signature
auth_hdr = "cscpp1-sha256 " + auth_user + " " + signature;
}