[7c0b03]: sighttpd-maxlistners-mutex.patch Maximize Restore History

Download this file

sighttpd-maxlistners-mutex.patch    179 lines (159 with data), 5.6 kB

  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
diff -rupN sighttpd/examples/sighttpd-oggstdin-maxlisteners.conf sighttpd.mod.maxlisteners//examples/sighttpd-oggstdin-maxlisteners.conf
--- sighttpd/examples/sighttpd-oggstdin-maxlisteners.conf 1970-01-01 01:00:00.000000000 +0100
+++ sighttpd.mod.maxlisteners//examples/sighttpd-oggstdin-maxlisteners.conf 2012-07-17 10:40:39.039138915 +0200
@@ -0,0 +1,19 @@
+
+# Port to listen on
+Listen 3000
+
+# Maximum allowed clients
+MaxListeners 10
+
+# Static text configuration
+<StaticText>
+Path "/info"
+Text "Sighttpd: Ogg Vorbis on stdin (/stream.ogg)"
+</StaticText>
+
+# Streaming Ogg Vorbis from stdin, using the special
+# OggStdin module that caches Ogg Vorbis headers
+<OggStdin>
+ Path "/stream.ogg"
+ Type "audio/ogg"
+</OggStdin>
diff -rupN sighttpd/src/http-response.c sighttpd.mod.maxlisteners//src/http-response.c
--- sighttpd/src/http-response.c 2012-06-15 18:00:02.188820357 +0200
+++ sighttpd.mod.maxlisteners//src/http-response.c 2012-07-17 10:40:39.039138915 +0200
@@ -93,6 +93,12 @@ respond_method_not_allowed (const char *
}
static void
+respond_service_unavailable (const char ** status_line, params_t ** response_headers)
+{
+ *status_line = http_status_line (HTTP_STATUS_SERVICE_UNAVAILABLE);
+}
+
+static void
respond (struct sighttpd_child * schild, http_request * request, params_t * request_headers)
{
params_t * response_headers;
@@ -100,22 +106,25 @@ respond (struct sighttpd_child * schild,
response_headers = response_headers_new ();
- switch (request->method) {
- case HTTP_METHOD_HEAD:
- case HTTP_METHOD_GET:
- respond_get_head (schild, request, request_headers, &status_line, &response_headers);
- break;
- default:
- respond_method_not_allowed (&status_line, &response_headers);
- break;
- }
+ switch (request->method) {
+ case HTTP_METHOD_HEAD:
+ case HTTP_METHOD_GET:
+ if(schild->service_unavailable)
+ respond_service_unavailable (&status_line, &response_headers);
+ else
+ respond_get_head (schild, request, request_headers, &status_line, &response_headers);
+ break;
+ default:
+ respond_method_not_allowed (&status_line, &response_headers);
+ break;
+ }
write (schild->accept_fd, status_line, strlen(status_line));
params_writefd (schild->accept_fd, response_headers);
log_access (request, request_headers, response_headers);
- if (request->method == HTTP_METHOD_GET) {
+ if (request->method == HTTP_METHOD_GET && schild->service_unavailable == 0 ) {
respond_get_body (schild, request, request_headers);
}
diff -rupN sighttpd/src/sighttpd.c sighttpd.mod.maxlisteners//src/sighttpd.c
--- sighttpd/src/sighttpd.c 2012-06-15 18:00:02.198820371 +0200
+++ sighttpd.mod.maxlisteners//src/sighttpd.c 2012-07-17 10:41:18.829197266 +0200
@@ -6,6 +6,7 @@
#include "config.h"
#endif
+#include <pthread.h>
#include <stdio.h>
#include <netdb.h>
@@ -26,6 +27,7 @@ struct sighttpd * sighttpd_init (struct
{
struct sighttpd * sighttpd;
const char *portname;
+ const char *max_listeners;
int port;
if ((sighttpd = malloc (sizeof(*sighttpd))) == NULL)
@@ -51,6 +53,17 @@ struct sighttpd * sighttpd_init (struct
port = atoi(portname);
}
+ pthread_mutex_init (&sighttpd->listeners_mutex, NULL);
+
+ max_listeners = dictionary_lookup (cfg->dictionary, "MaxListeners");
+ if (max_listeners == NULL) {
+ sighttpd->max_listeners = 0;
+ } else {
+ sighttpd->max_listeners = atoi(portname);
+ }
+
+ sighttpd->cur_listeners = 0;
+
sighttpd->port = port;
sighttpd->resources = cfg->resources;
@@ -67,6 +80,7 @@ struct sighttpd * sighttpd_init (struct
void sighttpd_close (struct sighttpd * sighttpd)
{
list_free_with (sighttpd->resources, resource_delete);
+ pthread_mutex_destroy (&sighttpd->listeners_mutex);
free (sighttpd);
}
@@ -78,6 +92,17 @@ sighttpd_child_new (struct sighttpd * si
if ((schild = malloc (sizeof(*schild))) == NULL)
return NULL;
+ pthread_mutex_lock (&sighttpd->listeners_mutex);
+
+ sighttpd->cur_listeners ++;
+
+ if( sighttpd->max_listeners > 0 && sighttpd->cur_listeners > sighttpd->max_listeners )
+ schild->service_unavailable = 1;
+ else
+ schild->service_unavailable = 0;
+
+ pthread_mutex_unlock (&sighttpd->listeners_mutex);
+
schild->sighttpd = sighttpd;
schild->accept_fd = accept_fd;
@@ -88,5 +113,13 @@ void
sighttpd_child_destroy (struct sighttpd_child * schild)
{
close (schild->accept_fd);
+
+ pthread_mutex_lock (&schild->sighttpd->listeners_mutex);
+
+ if( schild->sighttpd->cur_listeners > 0 )
+ schild->sighttpd->cur_listeners --;
+
+ pthread_mutex_unlock (&schild->sighttpd->listeners_mutex);
+
free (schild);
}
diff -rupN sighttpd/src/sighttpd.h sighttpd.mod.maxlisteners//src/sighttpd.h
--- sighttpd/src/sighttpd.h 2012-06-15 18:00:02.198820371 +0200
+++ sighttpd.mod.maxlisteners//src/sighttpd.h 2012-07-17 10:40:39.039138915 +0200
@@ -1,17 +1,22 @@
#ifndef __SIGHTTPD_H__
#define __SIGHTTPD_H__
+#include <pthread.h>
#include "cfg-read.h"
#include "list.h"
struct sighttpd {
int port;
list_t * resources;
+ pthread_mutex_t listeners_mutex;
+ int cur_listeners;
+ int max_listeners;
};
struct sighttpd_child {
struct sighttpd * sighttpd;
int accept_fd;
+ int service_unavailable;
};
struct sighttpd * sighttpd_init (struct cfg * cfg);