forked from eantcal/thttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpSocket.h
executable file
·119 lines (89 loc) · 2.79 KB
/
HttpSocket.h
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
//
// This file is part of thttpd
// Copyright (c) Antonino Calderone ([email protected])
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
//
/* -------------------------------------------------------------------------- */
/// \file HttpSocket.h
/// \brief Declaration of HTTP classes
/* -------------------------------------------------------------------------- */
#ifndef __HTTP_SOCKET_H__
#define __HTTP_SOCKET_H__
/* -------------------------------------------------------------------------- */
#include "TcpSocket.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include "config.h"
#include <string>
/* -------------------------------------------------------------------------- */
// HttpResponse
/* -------------------------------------------------------------------------- */
/**
* This class represents an HTTP connection between a client and a server.
*/
class HttpSocket {
private:
TcpSocket::Handle _socketHandle;
bool _connUp = true;
HttpRequest::Handle recv();
int _connectionTimeOut = HTTP_CONNECTION_TIMEOUT; // secs
public:
HttpSocket(int connectionTimeout = HTTP_CONNECTION_TIMEOUT) noexcept :
_connectionTimeOut(connectionTimeout)
{}
HttpSocket(const HttpSocket&) = default;
/**
* Construct the HTTP connection starting from TCP connected-socket handle.
*/
HttpSocket(TcpSocket::Handle handle)
: _socketHandle(handle)
{
}
/**
* Assigns a new TCP connected socket handle to this HTTP socket.
*/
HttpSocket& operator=(TcpSocket::Handle handle);
/**
* Returns TCP socket handle
*/
operator TcpSocket::Handle() const {
return _socketHandle;
}
/**
* Receives an HTTP request from remote peer.
* @param the handle of http request object
*/
HttpSocket& operator>>(HttpRequest::Handle& handle) {
handle = recv();
return *this;
}
/**
* Returns false if last recv/send operation detected
* that connection was down; true otherwise.
*/
explicit operator bool() const {
return _connUp;
}
/**
* Send a response to remote peer.
* @param response The HTTP response
*/
HttpSocket& operator<<(const HttpResponse& response);
/**
* Send a response to remote peer.
* @param response The HTTP response
*/
int sendFile(const std::string& fileName) {
return _socketHandle->sendFile(fileName);
}
/*
* Return connection timeout interval in seconds
*/
const int& getConnectionTimeout() const noexcept {
return _connectionTimeOut;
}
};
/* -------------------------------------------------------------------------- */
#endif // __HTTP_SOCKET_H__