GNU libmicrohttpd  0.9.63
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  _("Attempted 'upgrade' connection on daemon without MHD_ALLOW_UPGRADE option!\n"));
57 #endif
58  return MHD_SC_UPGRADE_ON_DAEMON_WITH_UPGRADE_DISALLOWED;
59  }
60 #endif /* UPGRADE_SUPPORT */
61  request->response = response;
62 #if defined(_MHD_HAVE_SENDFILE)
63  if ( (-1 == response->fd)
64 #if HTTPS_SUPPORT
65  || (NULL != daemon->tls_api)
66 #endif
67  )
68  request->resp_sender = MHD_resp_sender_std;
69  else
70  request->resp_sender = MHD_resp_sender_sendfile;
71 #endif /* _MHD_HAVE_SENDFILE */
72 
73  if ( (MHD_METHOD_HEAD == request->method) ||
74  (MHD_HTTP_OK > response->status_code) ||
75  (MHD_HTTP_NO_CONTENT == response->status_code) ||
76  (MHD_HTTP_NOT_MODIFIED == response->status_code) )
77  {
78  /* if this is a "HEAD" request, or a status code for
79  which a body is not allowed, pretend that we
80  have already sent the full message body. */
81  request->response_write_position = response->total_size;
82  }
83  if ( (MHD_REQUEST_HEADERS_PROCESSED == request->state) &&
84  ( (MHD_METHOD_POST == request->method) ||
85  (MHD_METHOD_PUT == request->method) ) )
86  {
87  /* response was queued "early", refuse to read body / footers or
88  further requests! */
89  request->connection->read_closed = true;
91  }
92  if (! request->in_idle)
93  (void) MHD_request_handle_idle_ (request);
94  return MHD_SC_OK;
95 }
96 
97 
115 _MHD_EXTERN const struct MHD_Action *
117  enum MHD_Bool destroy_after_use)
118 {
119  response->action.action = &response_action;
120  response->action.action_cls = response;
121  if (! destroy_after_use)
122  {
123  MHD_mutex_lock_chk_ (&response->mutex);
124  response->reference_count++;
125  MHD_mutex_unlock_chk_ (&response->mutex);
126  }
127  return &response->action;
128 }
129 
130 /* end of action_from_response */
uint64_t total_size
Definition: internal.h:1639
#define MHD_mutex_unlock_chk_(pmutex)
Definition: mhd_locks.h:177
static enum MHD_StatusCode response_action(void *cls, struct MHD_Request *request)
#define MHD_HTTP_NOT_MODIFIED
Definition: microhttpd.h:329
bool in_idle
Definition: internal.h:562
internal shared structures
_MHD_EXTERN const struct MHD_Action * MHD_action_from_response(struct MHD_Response *response, enum MHD_Bool destroy_after_use)
enum MHD_REQUEST_STATE state
Definition: internal.h:546
struct MHD_Daemon * daemon
Definition: internal.h:369
ActionCallback action
Definition: internal.h:1551
enum MHD_HTTP_StatusCode status_code
Definition: internal.h:1666
#define MHD_HTTP_NO_CONTENT
Definition: microhttpd.h:317
unsigned int reference_count
Definition: internal.h:1672
#define MHD_HTTP_OK
Definition: microhttpd.h:313
struct MHD_Action action
Definition: internal.h:1572
#define _MHD_EXTERN
Definition: mhd_options.h:51
function to call event handlers based on event mask
#define NULL
Definition: reason_phrase.c:30
bool disallow_upgrade
Definition: internal.h:1471
enum MHD_Method method
Definition: internal.h:551
struct MHD_Response * response
Definition: internal.h:380
#define MHD_mutex_lock_chk_(pmutex)
Definition: mhd_locks.h:151
bool read_closed
Definition: internal.h:789
void * action_cls
Definition: internal.h:1556
uint64_t response_write_position
Definition: internal.h:526
MHD_mutex_ mutex
Definition: internal.h:1634
#define _(String)
Definition: mhd_options.h:42
struct MHD_Connection * connection
Definition: internal.h:374
volatile bool shutdown
Definition: internal.h:1523
bool MHD_request_handle_idle_(struct MHD_Request *request)