aboutsummaryrefslogtreecommitdiff
path: root/src/lib/action_suspend.c
blob: c176e4949e69dd2130a8ff696f0cd4032d6cfc7a (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
/*
  This file is part of libmicrohttpd
  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff

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

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * @file lib/action_suspend.c
 * @brief implementation of MHD_action_suspend()
 * @author Christian Grothoff
 */
#include "internal.h"


/**
 * The suspend action is being run.  Suspend handling
 * of the given request.
 *
 * @param cls NULL
 * @param request the request to apply the action to
 * @return #MHD_SC_OK on success
 */
static enum MHD_StatusCode
suspend_action (void *cls,
                struct MHD_Request *request)
{
  (void) cls;
  struct MHD_Connection *connection = request->connection;
  struct MHD_Daemon *daemon = connection->daemon;

  MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
  if (connection->resuming)
  {
    /* suspending again while we didn't even complete resuming yet */
    connection->resuming = false;
    MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
    return MHD_SC_OK;
  }
  if (daemon->threading_mode != MHD_TM_THREAD_PER_CONNECTION)
  {
    if (connection->connection_timeout ==
        daemon->connection_default_timeout)
      XDLL_remove (daemon->normal_timeout_head,
                   daemon->normal_timeout_tail,
                   connection);
    else
      XDLL_remove (daemon->manual_timeout_head,
                   daemon->manual_timeout_tail,
                   connection);
  }
  DLL_remove (daemon->connections_head,
              daemon->connections_tail,
              connection);
  mhd_assert (! connection->suspended);
  DLL_insert (daemon->suspended_connections_head,
              daemon->suspended_connections_tail,
              connection);
  connection->suspended = true;
#ifdef EPOLL_SUPPORT
  if (MHD_ELS_EPOLL == daemon->event_loop_syscall)
  {
    if (0 != (connection->epoll_state & MHD_EPOLL_STATE_IN_EREADY_EDLL))
    {
      EDLL_remove (daemon->eready_head,
                   daemon->eready_tail,
                   connection);
      connection->epoll_state &= ~MHD_EPOLL_STATE_IN_EREADY_EDLL;
    }
    if (0 != (connection->epoll_state & MHD_EPOLL_STATE_IN_EPOLL_SET))
    {
      if (0 != epoll_ctl (daemon->epoll_fd,
                          EPOLL_CTL_DEL,
                          connection->socket_fd,
                          NULL))
        MHD_PANIC (_ ("Failed to remove FD from epoll set\n"));
      connection->epoll_state &= ~MHD_EPOLL_STATE_IN_EPOLL_SET;
    }
    connection->epoll_state |= MHD_EPOLL_STATE_SUSPENDED;
  }
#endif
  MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex);
  return MHD_SC_OK;
}


/**
 * Suspend handling of network data for a given request.  This can
 * be used to dequeue a request from MHD's event loop for a while.
 *
 * If you use this API in conjunction with a internal select or a
 * thread pool, you must set the option #MHD_USE_ITC to
 * ensure that a resumed request is immediately processed by MHD.
 *
 * Suspended requests continue to count against the total number of
 * requests allowed (per daemon, as well as per IP, if such limits
 * are set).  Suspended requests will NOT time out; timeouts will
 * restart when the request handling is resumed.  While a
 * request is suspended, MHD will not detect disconnects by the
 * client.
 *
 * The only safe time to suspend a request is from either a
 * #MHD_RequestHeaderCallback, #MHD_UploadCallback, or a
 * #MHD_RequestfetchResponseCallback.  Suspending a request
 * at any other time will cause an assertion failure.
 *
 * Finally, it is an API violation to call #MHD_daemon_stop() while
 * having suspended requests (this will at least create memory and
 * socket leaks or lead to undefined behavior).  You must explicitly
 * resume all requests before stopping the daemon.
 *
 * @return action to cause a request to be suspended.
 */
const struct MHD_Action *
MHD_action_suspend (void)
{
  static const struct MHD_Action suspend = {
    .action = &suspend_action,
    .action_cls = NULL
  };

  return &suspend;
}

/* end of action_suspend.c */