GNU libmicrohttpd  0.9.72
action_from_response.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
25 #include "internal.h"
27 
28 
37 static enum MHD_StatusCode
38 response_action (void *cls,
39  struct MHD_Request *request)
40 {
41  struct MHD_Response *response = cls;
42  struct MHD_Daemon *daemon = request->daemon;
43 
44  /* If daemon was shut down in parallel,
45  * response will be aborted now or on later stage. */
46  if (daemon->shutdown)
47  return MHD_SC_DAEMON_ALREADY_SHUTDOWN;
48 
49 #ifdef UPGRADE_SUPPORT
50  if ( (NULL != response->upgrade_handler) &&
51  daemon->disallow_upgrade)
52  {
53 #ifdef HAVE_MESSAGES
54  MHD_DLOG (daemon,
55  MHD_SC_UPGRADE_ON_DAEMON_WITH_UPGRADE_DISALLOWED,
56  _ (
57  "Attempted 'upgrade' connection on daemon without MHD_ALLOW_UPGRADE option!\n"));
58 #endif
59  return MHD_SC_UPGRADE_ON_DAEMON_WITH_UPGRADE_DISALLOWED;
60  }
61 #endif /* UPGRADE_SUPPORT */
62  request->response = response;
63 #if defined(_MHD_HAVE_SENDFILE)
64  if ( (-1 == response->fd)
65 #if HTTPS_SUPPORT
66  || (NULL != daemon->tls_api)
67 #endif
68  )
69  request->resp_sender = MHD_resp_sender_std;
70  else
71  request->resp_sender = MHD_resp_sender_sendfile;
72 #endif /* _MHD_HAVE_SENDFILE */
73 
74  if ( (MHD_METHOD_HEAD == request->method) ||
75  (MHD_HTTP_OK > response->status_code) ||
76  (MHD_HTTP_NO_CONTENT == response->status_code) ||
77  (MHD_HTTP_NOT_MODIFIED == response->status_code) )
78  {
79  /* if this is a "HEAD" request, or a status code for
80  which a body is not allowed, pretend that we
81  have already sent the full message body. */
82  request->response_write_position = response->total_size;
83  }
84  if ( (MHD_REQUEST_HEADERS_PROCESSED == request->state) &&
85  ( (MHD_METHOD_POST == request->method) ||
86  (MHD_METHOD_PUT == request->method) ) )
87  {
88  /* response was queued "early", refuse to read body / footers or
89  further requests! */
90  request->connection->read_closed = true;
92  }
93  if (! request->in_idle)
94  (void) MHD_request_handle_idle_ (request);
95  return MHD_SC_OK;
96 }
97 
98 
116 _MHD_EXTERN const struct MHD_Action *
118  enum MHD_Bool destroy_after_use)
119 {
120  response->action.action = &response_action;
121  response->action.action_cls = response;
122  if (! destroy_after_use)
123  {
124  MHD_mutex_lock_chk_ (&response->mutex);
125  response->reference_count++;
126  MHD_mutex_unlock_chk_ (&response->mutex);
127  }
128  return &response->action;
129 }
130 
131 
132 /* end of action_from_response */
static enum MHD_StatusCode response_action(void *cls, struct MHD_Request *request)
_MHD_EXTERN const struct MHD_Action * MHD_action_from_response(struct MHD_Response *response, enum MHD_Bool destroy_after_use)
bool MHD_request_handle_idle_(struct MHD_Request *request)
function to call event handlers based on event mask
#define MHD_HTTP_OK
Definition: microhttpd.h:344
#define MHD_HTTP_NOT_MODIFIED
Definition: microhttpd.h:374
#define MHD_HTTP_NO_CONTENT
Definition: microhttpd.h:352
@ MHD_REQUEST_FOOTERS_RECEIVED
Definition: internal.h:269
@ MHD_REQUEST_HEADERS_PROCESSED
Definition: internal.h:242
#define MHD_mutex_unlock_chk_(pmutex)
Definition: mhd_locks.h:180
#define MHD_mutex_lock_chk_(pmutex)
Definition: mhd_locks.h:154
#define NULL
Definition: reason_phrase.c:30
#define _(String)
Definition: mhd_options.h:42
#define _MHD_EXTERN
Definition: mhd_options.h:50
internal shared structures
ActionCallback action
Definition: internal.h:1554
void * action_cls
Definition: internal.h:1559
bool read_closed
Definition: internal.h:792
bool disallow_upgrade
Definition: internal.h:1474
volatile bool shutdown
Definition: internal.h:1526
struct MHD_Response * response
Definition: internal.h:383
enum MHD_Method method
Definition: internal.h:554
bool in_idle
Definition: internal.h:565
uint64_t response_write_position
Definition: internal.h:529
struct MHD_Connection * connection
Definition: internal.h:377
struct MHD_Daemon * daemon
Definition: internal.h:372
enum MHD_REQUEST_STATE state
Definition: internal.h:549
enum MHD_HTTP_StatusCode status_code
Definition: internal.h:1669
struct MHD_Action action
Definition: internal.h:1575
unsigned int reference_count
Definition: internal.h:1675
MHD_mutex_ mutex
Definition: internal.h:1637
uint64_t total_size
Definition: internal.h:1642