-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.h
120 lines (91 loc) · 3.27 KB
/
client.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
120
//
// Created by heshamovic on 11/16/18.
//
#ifndef SIMPLE_HTTP_SERVER_CLIENT_CLIENT_H
#define SIMPLE_HTTP_SERVER_CLIENT_CLIENT_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <streambuf>
#include <istream>
#include <ostream>
#include <mutex>
#include <unistd.h>
#include <vector>
#include "util.h"
namespace client {
const std::string http_version_1_0 = "HTTP/1.0";
const std::string http_version_1_1 = "HTTP/1.1";
const std::string user_agent = "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0";;
const int ok_status = 200;
const int default_port = 80;
namespace net {
template<typename Char>
class basic_socketbuf
: public std::basic_streambuf<Char> {
public:
typedef Char char_type;
typedef std::basic_streambuf<char_type> buf_type;
typedef std::basic_ostream<char_type> stream_type;
typedef typename buf_type::int_type int_type;
typedef typename std::basic_streambuf<Char>::traits_type traits_type;
protected:
static const int char_size = sizeof(char_type);
static const int SIZE = 4096;
char_type obuf[SIZE];
char_type ibuf[SIZE];
int sock;
public:
basic_socketbuf();
virtual ~basic_socketbuf();
void set_socket(int sock);
int get_socket();
protected:
int output_buffer();
virtual int_type overflow(int_type c) {
if (c != traits_type::eof()) {
*buf_type::pptr() = c;
buf_type::pbump(1);
}
if (output_buffer() == traits_type::eof())
return traits_type::eof();
return c;
}
virtual int sync();
virtual int_type underflow() {
if (buf_type::gptr() < buf_type::egptr())
return *buf_type::gptr();
int num;
if ((num = recv(sock, reinterpret_cast<char *>(ibuf), SIZE * char_size, 0)) <= 0) {
return traits_type::eof();
}
buf_type::setg(ibuf, ibuf, ibuf + num);
return *buf_type::gptr();
}
};
typedef basic_socketbuf<char> socketbuf;
typedef basic_socketbuf<wchar_t> wsocketbuf;
template<typename Char>
class basic_socketstream
: public std::basic_iostream<Char> {
public:
typedef Char char_type;
typedef std::basic_iostream<char_type> stream_type;
typedef basic_socketbuf<char_type> buf_type;
std::mutex sock_mux;
protected:
buf_type buf;
public:
basic_socketstream();
basic_socketstream(int s);
void close_socket();
bool open(const std::string &host, uint16_t port);
};
typedef basic_socketstream<char> socketstream;
typedef basic_socketstream<wchar_t> wsocketstream;
}
void http1_0(const char *argv[]);
void http1_1(const char *argv[]);
void receive_thread(std::vector<std::string>, net::socketstream&);
}
#endif //SIMPLE_HTTP_SERVER_CLIENT_CLIENT_H