aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/session.h
blob: 24ddd641bc3bf6fbbc81cce6df8deef883e3bb8a (plain) (blame)
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
/*
     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 session.h
 * @brief  Methods for managing sessions
 * @author Daniel Pittman
 * @author Christian Grothoff
 * @version 0.1.0
 */

#ifndef SESSION_H
#define SESSION_H


struct MHD_Session {
  struct MHD_Session * next;

  struct MHD_Daemon * daemon;

  struct MHD_HTTP_Header * headers_received;
  
  struct MHD_Response * response;

  char * method;

  char * url;

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

  /**
   * Buffer for writing response.
   */
  void * 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;

};


/**
 * Obtain the select sets for this session.
 *
 * @return MHD_YES on success
 */
int 
MHD_session_get_fdset(struct MHD_Session * session,
		      fd_set * read_fd_set,
		      fd_set * write_fd_set,
		      fd_set * except_fd_set,
		      int * max_fd);


/**
 * This function handles a particular connection when it has been
 * determined that there is data to be read off a socket. All implementations
 * (multithreaded, external select, internal select) call this function
 * to handle reads. 
 */
int
MHD_session_handle_read(struct MHD_Session * session);
  

/**
 * This function was created to handle writes to sockets when it has been
 * determined that the socket can be written to. If there is no data
 * to be written, however, the function call does nothing. All implementations
 * (multithreaded, external select, internal select) call this function
 */
int
MHD_session_handle_write(struct MHD_Session * session);


#endif