summaryrefslogtreecommitdiff
path: root/src/daemon/internal.h
blob: 9a3ec5a2cd367a34091e6ca58025f8c2e8968bb2 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
     This file is part of libmicrohttpd
     (C) 2007 Daniel Pittman

     libmicrohttpd is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libmicrohttpd is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file internal.h
 * @brief  internal shared structures
 * @author Daniel Pittman
 * @author Christian Grothoff
 * @version 0.1.0
 */

#ifndef INTERNAL_H
#define INTERNAL_H


#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <netinet/in.h>

#include "microhttpd.h"
#include "config.h"

#define MHD_MAX_BUF_SIZE 2048

#define MAX(a,b) ((a)<(b)) ? (b) : (a)


/**
 * fprintf-like helper function for logging debug
 * messages.
 */
void MHD_DLOG(const struct MHD_Daemon * daemon,
	      const char * format,
	      ...);


/**
 * Header or cookie in HTTP request or response.
 */
struct MHD_HTTP_Header {
  struct MHD_HTTP_Header * next;

  char * header;

  char * value;

  enum MHD_ValueKind kind;
};


struct MHD_Access_Handler {
  struct MHD_Access_Handler * next;

  char * uri_prefix;

  MHD_AccessHandlerCallback dh;

  void * dh_cls;
};


/**
 * Representation of a response.
 */ 
struct MHD_Response {

  /**
   * Headers to send for the response.  Initially
   * the linked list is created in inverse order;
   * the order should be inverted before sending!
   */
  struct MHD_HTTP_Header * first_header;

  /**
   * Buffer pointing to data that we are supposed
   * to send as a response.
   */
  char * data;

  /**
   * Closure to give to the content reader
   * free callback.
   */ 
  void * crc_cls;
 
  /**
   * How do we get more data?  NULL if we are
   * given all of the data up front.
   */
  MHD_ContentReaderCallback crc;

  /**
   * NULL if data must not be freed, otherwise
   * either user-specified callback or "&free".
   */
  MHD_ContentReaderFreeCallback crfc;

  /**
   * Mutex to synchronize access to data/size and
   * reference counts.
   */
  pthread_mutex_t mutex;

  /**
   * Reference count for this response.  Free
   * once the counter hits zero.
   */
  unsigned int reference_count;
  
  /**
   * Set to -1 if size is not known.
   */
  size_t total_size;

  /**
   * Size of data.
   */
  size_t data_size;

  /**
   * At what offset in the stream is the
   * beginning of data located?
   */
  size_t data_start;
  
};



struct MHD_Session {
  struct MHD_Session * next;

  struct MHD_Daemon * daemon;

  struct MHD_HTTP_Header * headers_received;
  
  struct MHD_Response * response;

  /**
   * Request method.  Should be GET/POST/etc.
   */
  char * method;

  /**
   * Requested URL (everything after "GET" only).
   */
  char * url;

  /**
   * Buffer for reading requests.
   */
  char * read_buffer;

  /**
   * Buffer for writing response.
   */
  char * write_buffer;
  
  /**
   * Foreign address (of length addr_len).
   */
  struct sockaddr_in * addr;

  /**
   * Thread for this session (if we are using
   * one thread per connection).
   */
  pthread_t pid;

  size_t read_buffer_size;

  size_t readLoc;

  size_t write_buffer_size;

  size_t writeLoc;

  /**
   * Current write position in the actual response
   * (excluding headers, content only; should be 0
   * while sending headers).
   */
  size_t messagePos;

  /**
   * Remaining (!) number of bytes in the upload.
   * Set to -1 for unknown (connection will close
   * to indicate end of upload).
   */
  size_t uploadSize;

  /**
   * Length of the foreign address.
   */
  socklen_t addr_len;

  /**
   * Socket for this connection.  Set to -1 if
   * this connection has died (daemon should clean
   * up in that case).
   */
  int socket_fd;
  
  /**
   * Have we finished receiving all of the headers yet?
   * Set to 1 once we are done processing all of the
   * headers.  Note that due to pipelining, it is
   * possible that the NEXT request is already 
   * (partially) waiting in the read buffer.
   */
  int headersReceived;

  /**
   * Have we finished receiving the data from a
   * potential file-upload? 
   */
  int bodyReceived;

  /**
   * Have we finished sending all of the headers yet?
   */
  int headersSent;

  /**
   * HTTP response code.  Only valid if response object
   * is already set.
   */
  unsigned int responseCode;

};



struct MHD_Daemon {

  struct MHD_Access_Handler * handlers;

  struct MHD_Access_Handler default_handler;

  struct MHD_Session * connections;
  
  MHD_AcceptPolicyCallback apc;

  void * apc_cls;

  /**
   * PID of the select thread (if we have internal select)
   */
  pthread_t pid;

  /**
   * Listen socket.
   */
  int socket_fd;

  /**
   * Are we shutting down?
   */
  int shutdown;

  /**
   * Daemon's options.
   */
  enum MHD_OPTION options;

  unsigned short port;

};


#endif