1 |
|
/* ============================================================================ |
2 |
|
* Douglas Thrift's Search Engine License |
3 |
|
* |
4 |
< |
* Copyright (C) 2002, Douglas Thrift. All Rights Reserved. |
4 |
> |
* Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved. |
5 |
|
* Redistribution and use in source and binary forms, with or without |
6 |
|
* modification, are permitted provided that the following conditions are met: |
7 |
|
* |
46 |
|
// |
47 |
|
// Douglas Thrift |
48 |
|
// |
49 |
< |
// HttpHandler.h |
49 |
> |
// $Id: HttpHandler.h,v 1.18 2003/07/20 01:46:12 douglas Exp $ |
50 |
|
|
51 |
|
#ifndef _HttpHandler_h_ |
52 |
|
#define _HttpHandler_h_ |
56 |
|
|
57 |
|
// Lovely C Sockets! |
58 |
|
#ifdef _WIN32 |
59 |
– |
|
59 |
|
// Windows Sockets |
60 |
|
#include <winsock2.h> |
61 |
|
#include <windows.h> |
62 |
|
|
63 |
+ |
inline int getpid() { return GetCurrentProcessId(); } |
64 |
|
#else |
65 |
– |
|
65 |
|
// BSD Sockets |
66 |
< |
#include <unistd.h> |
68 |
< |
#include <sys/types.h> |
69 |
< |
#include <sys/socket.h> |
70 |
< |
#include <netinet/in.h> |
71 |
< |
#include <netdb.h> |
66 |
> |
typedef int SOCKET; |
67 |
|
|
68 |
|
#define INVALID_SOCKET -1 |
69 |
|
#define SOCKET_ERROR -1 |
70 |
|
|
76 |
– |
typedef int SOCKET; |
77 |
– |
|
71 |
|
inline int closesocket(SOCKET s) { return close(s); } |
79 |
– |
|
72 |
|
#endif // _WIN32 |
73 |
|
|
74 |
+ |
#ifdef _OpenSSL_ |
75 |
+ |
#include <openssl/ssl.h> |
76 |
+ |
#include <openssl/err.h> |
77 |
+ |
#include <openssl/rand.h> |
78 |
+ |
#endif |
79 |
+ |
|
80 |
|
class HttpHandler |
81 |
|
{ |
82 |
|
private: |
86 |
|
WSADATA data; |
87 |
|
#endif // _WIN32 |
88 |
|
SOCKET http; |
89 |
+ |
#ifdef _OpenSSL_ |
90 |
+ |
SSL_CTX* ctx; |
91 |
+ |
SSL* ssl; |
92 |
+ |
bool tls; |
93 |
+ |
#endif |
94 |
|
char* buffer; |
95 |
< |
unsigned begin; |
96 |
< |
string page; |
95 |
> |
bool binary; |
96 |
> |
stringstream page; |
97 |
|
string type; |
98 |
|
unsigned length; |
99 |
|
string location; |
100 |
|
bool chunked; |
101 |
+ |
void populate(); |
102 |
|
void putline(const string line = ""); |
103 |
|
string getline(); |
104 |
|
void error(const string& prefix, bool host = false); |
105 |
+ |
#ifdef _OpenSSL_ |
106 |
+ |
void error(const string& prefix, int number); |
107 |
+ |
bool starttls(); |
108 |
+ |
#endif |
109 |
|
public: |
110 |
|
HttpHandler(); |
111 |
|
~HttpHandler(); |
112 |
< |
bool handle(URL& url, bool head = false); |
113 |
< |
HttpHandler& getline(string& line, char endline = '\n'); |
114 |
< |
bool good(); |
112 |
> |
bool handle(URL& url, const string referer = "", bool head = false); |
113 |
> |
iostream& pageStream() { return page; } |
114 |
> |
istream& getline(string& line) { return std::getline(page, line); } |
115 |
> |
istream& getline(string& line, char end) { return std::getline(page, line, |
116 |
> |
end); } |
117 |
> |
bool good() { return page.good(); } |
118 |
|
void clear(); |
119 |
|
string contentType() { return type; } |
120 |
|
unsigned contentLength() { return length; } |