This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.cc
154 lines (125 loc) · 3.26 KB
/
http.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* HTTP/1.0 for streams sockets - implementation
* Copyright(c) 2003-2018 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/
// interface
#include "http.hh"
// local headers
#include "urlencode.hh"
#include "base64.hh"
// system headers
#include <stdexcept>
#include <memory>
#include <string>
using std::string;
using std::getline;
#include <sstream>
using std::istringstream;
// c system headers
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
// implementation
namespace Http
{
namespace Proto
{
// definitions
const char* proto = "http";
const char* port = "80";
const char* protoTy = "tcp";
const char* version = "HTTP/1.0";
const char* endl = "\r\n";
const char* get = "GET";
const char* authorization = "Authorization";
const char* basic = "Basic";
const char* location = "Location";
}
std::string
Auth::basicHeader() const
{
return (string(Proto::authorization) + ": " + Proto::basic + " " +
base64Encode(user + ":" + pass));
}
Http::Http(const char* host, const char* port, const timeval* timeout)
{
this->host = strdup(host);
this->port = strdup(port? port: Proto::port);
if(!timeout)
this->timeout = NULL;
else
{
this->timeout = new timeval;
*this->timeout = *timeout;
}
}
Http::~Http()
{
free(host);
free(port);
if(timeout)
delete timeout;
}
void
Http::readReply(Socket& s, Reply& reply)
{
string proto;
char buf[Proto::hdrLen];
size_t bufLen;
// read all lines
while((bufLen = s.gets(buf, Proto::hdrLen)) > Proto::endlSz)
{
if(reply.headers || !proto.size())
{
// cut trailing newlines from the buffer
while(strchr(Proto::endl, buf[bufLen - 1]))
buf[--bufLen] = 0;
if(!proto.size())
proto = string(buf, bufLen);
else if(reply.headers)
reply.headers->push_back(string(buf, bufLen));
}
}
if(bufLen < Proto::endlSz)
throw std::runtime_error("premature end of http headers");
// parse the first protocol line
istringstream in(proto);
in >> reply.proto;
in >> reply.code;
in >> std::ws;
getline(in, reply.description);
}
Socket*
Http::gen(const char* act, const char* path,
Reply& reply, const Header* headers)
{
using std::auto_ptr;
auto_ptr<Socket> s(new Socket(host, port, timeout));
// request
string req;
req = string(act) + " " + urlEncode(path) + " " +
Proto::version + Proto::endl;
req += string("Host: ") + host + Proto::endl;
// headers
if(headers)
for(Header::const_iterator it = headers->begin();
it != headers->end(); ++it)
req += *it + Proto::endl;
// final newline
req += Proto::endl;
s->writen(req.c_str(), req.size());
// fetch the answer headers
readReply(*s, reply);
return s.release();
}
Socket*
Http::get(const char* file, Reply& reply, const Header* headers)
{
// we used to close the write end here, as HTTP/1.0 requests really end
// here and we're very happy to relase some resources. Unfortunately,
// SHOUTcast > 1.9 servers simply interrupt the connection when receiving
// the notification. "We fixed all known bugs in 1.9". Aisee...
return gen(Proto::get, file, reply, headers);
}
}