EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
HTTPServer.h
1 // -*- c++ -*-
2 // $Id: HTTPServer.h,v 1.21 2006/05/01 18:10:32 kasemir Exp $
3 //
4 // Please refer to NOTICE.txt,
5 // included as part of this distribution,
6 // for legal information.
7 //
8 // kasemir@lanl.gov
9 
10 #if !defined(HTTP_SERVER_H_)
11 #define HTTP_SERVER_H_
12 
13 // epics
14 #include <epicsTime.h>
15 #include <epicsThread.h>
16 
17 // tools
18 #include "tools/ToolsConfig.h"
19 #include "tools/NetTools.h"
20 #include "tools/Guard.h"
21 #include "tools/AutoPtr.h"
22 
23 // engine
24 #include "engine/HTMLPage.h"
25 
26 // undef: Nothing
27 // 1: Start/Stop
28 // 2: Show Client IPs
29 // 3: Connect & cleanup
30 // 4: Requests
31 // 5: whatever
32 #define HTTPD_DEBUG 2
33 
34 // Maximum number of clients that we accept.
35 // This includes connections that are "done"
36 // and need to be cleaned up
37 // (which happens every HTTPD_TIMEOUT seconds).
38 // Since most clients lock the engine to get the
39 // list of channels,
40 // really only one client can run at a time.
41 // Allowing a few more to queue up is OK,
42 // but a long list doesn't make any sense.
43 #define MAX_NUM_CLIENTS 3
44 
45 // These timeouts influence how quickly the server reacts,
46 // including how fast the whole engine can be shut down.
47 
48 // Timeout for server to check for new client
49 #define HTTPD_TIMEOUT 1
50 
51 // Client uses timeout for each read
52 #define HTTPD_READ_TIMEOUT 1
53 
54 // HTTP clients older than this total timeout are killed
55 #define HTTPD_CLIENT_TIMEOUT 10
56 
62 typedef struct {
63 
65  typedef void (*PathHandler) (class HTTPClientConnection *connection,
66  const stdString &full_path,
67  void *user_arg);
68 
69  const char *path;
70  size_t path_len;
71  PathHandler handler;
73 
85 class HTTPServer : public epicsThreadRunable {
86 
87 public:
88 
97  HTTPServer(short port, PathHandlerList* handlers, void* user_arg);
98 
99  virtual ~HTTPServer();
100 
102  void start();
103 
105  void run();
106 
108  void serverinfo(SOCKET socket);
109 
112  { return handlers; }
113 
115  void *getUserArg() const
116  { return user_arg; }
117 
119  bool isShuttingDown() const
120  { return go == false; }
121 
122 private:
123 
124  epicsThread thread;
125  bool go;
126  SOCKET socket;
127  PathHandlerList *handlers;
128  void *user_arg;
129  size_t total_clients;
130  double client_duration; // seconds; averaged
131  OrderedMutex client_list_mutex;
133 
134  // Create a new HTTPClientConnection, add to 'clients'.
135  void start_client(SOCKET peer);
136 
137  // returns # of clients that are still active
138  size_t client_cleanup();
139 
140  void reject(SOCKET socket);
141 };
142 
150 class HTTPClientConnection : public epicsThreadRunable
151 {
152 public:
153 
155  HTTPClientConnection(HTTPServer *server, SOCKET socket, int num);
156 
158  virtual ~HTTPClientConnection();
159 
162  { return server; }
163 
165  SOCKET getSocket()
166  { return socket; }
167 
169  size_t getNum()
170  { return num; }
171 
173  bool isDone()
174  { return done; }
175 
177  void error(const std::string& message);
178 
180  void pathError(const std::string& path);
181 
183  void start()
184  { thread.start(); }
185 
187  void run();
188 
190  const epicsTime& getBirthTime()
191  { return birthtime; }
192 
194  double getRuntime() const
195  { return runtime; }
196 
198  void join();
199 
200 private:
201  epicsThread thread; // .. that handles this connection
202  HTTPServer *server;
203  epicsTime birthtime;
204  size_t num; // unique sequence number of this conn.
205  bool done; // has run() finished running?
206  SOCKET socket;
207  stdVector<stdString> input_line;
208  char line[2048];
209  unsigned int dest; // index of next unused char in _line
210  double runtime;
211 
212  // Result: done, i.e. connection can be closed?
213  bool handleInput();
214 
215  // return: full request that I can handle?
216  bool analyzeInput();
217 
218  void dumpInput(HTMLPage &page);
219 };
220 
221 #endif // !defined(HTTP_SERVER_H_)
HTTPServer(short port, PathHandlerList *handlers, void *user_arg)
Create a HTTPServer.
SOCKET getSocket()
Returns socket.
Definition: HTTPServer.h:165
virtual ~HTTPClientConnection()
Destructor.
const char * path
Path for this handler.
Definition: HTTPServer.h:69
A mutex with informational name and lock order.
Definition: OrderedMutex.h:34
An auto-pointer for arrays.
Definition: AutoPtr.h:120
void start()
Start accepting connections (launch thread).
Handler for a HTTPServer&#39;s client.
Definition: HTTPServer.h:150
Helper for printing a web page to a socket.
Definition: HTMLPage.h:31
void serverinfo(SOCKET socket)
Dump HTML page with server info to socket.
HTTPServer * getServer()
Returns server.
Definition: HTTPServer.h:161
double getRuntime() const
Definition: HTTPServer.h:194
bool isShuttingDown() const
Definition: HTTPServer.h:119
An in-memory web server.
Definition: HTTPServer.h:85
const epicsTime & getBirthTime()
Definition: HTTPServer.h:190
void run()
Part of the epicsThreadRunable interface.
HTTPClientConnection(HTTPServer *server, SOCKET socket, int num)
Constructor.
void * getUserArg() const
Definition: HTTPServer.h:115
void error(const std::string &message)
Predefined PathHandler.
size_t getNum()
Definition: HTTPServer.h:169
size_t path_len
Relevant portion of path to check (if &gt; 0)
Definition: HTTPServer.h:70
void pathError(const std::string &path)
Predefined PathHandler.
Used by HTTPClientConnection to dispatch client requests.
Definition: HTTPServer.h:62
bool isDone()
Checks if done.
Definition: HTTPServer.h:173
void start()
Start.
Definition: HTTPServer.h:183
PathHandlerList * getHandlers() const
Definition: HTTPServer.h:111
PathHandler handler
Handler to call.
Definition: HTTPServer.h:71