pitchfork 0.18.2 → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -180,12 +180,6 @@ http_version(struct http_parser *hp, const char *ptr, size_t len)
180
180
  rb_hash_aset(hp->env, g_http_version, v);
181
181
  }
182
182
 
183
- static inline void hp_invalid_if_trailer(struct http_parser *hp)
184
- {
185
- if (HP_FL_TEST(hp, INTRAILER))
186
- parser_raise(eHttpParserError, "invalid Trailer");
187
- }
188
-
189
183
  static void write_cont_value(struct http_parser *hp,
190
184
  char *buffer, const char *p)
191
185
  {
@@ -236,16 +230,42 @@ static int is_chunked(VALUE v)
236
230
  }
237
231
 
238
232
  static void write_value(VALUE self, struct http_parser *hp,
239
- const char *buffer, const char *p)
233
+ char *buffer, const char *p)
240
234
  {
241
- VALUE f = find_common_field(PTR_TO(start.field), hp->s.field_len);
235
+ VALUE f;
236
+ char *field = PTR_TO(start.field);
237
+ size_t i;
238
+ VALUE target = hp->env;
242
239
  VALUE v;
243
240
  VALUE e;
241
+ int in_trailer = HP_FL_TEST(hp, INTRAILER);
242
+
243
+ /* Reject ambiguous framing headers before normalizing dashes for Rack. */
244
+ if (CONST_MEM_EQ("CONTENT_LENGTH", field, hp->s.field_len))
245
+ parser_raise(eHttpParserError, "invalid Content-Length");
246
+ if (CONST_MEM_EQ("TRANSFER_ENCODING", field, hp->s.field_len))
247
+ parser_raise(eHttpParserError, "invalid Transfer-Encoding");
248
+
249
+ if (memchr(field, '_', hp->s.field_len)) {
250
+ VALIDATE_MAX_LENGTH(hp->s.field_len, FIELD_NAME);
251
+ VALIDATE_MAX_LENGTH(LEN(mark, p), FIELD_VALUE);
252
+ RB_OBJ_WRITE(self, &hp->cont, Qnil);
253
+ return;
254
+ }
255
+
256
+ for (i = 0; i < hp->s.field_len; i++)
257
+ if (field[i] == '-')
258
+ field[i] = '_';
259
+
260
+ f = find_common_field(field, hp->s.field_len);
244
261
 
245
262
  VALIDATE_MAX_LENGTH(LEN(mark, p), FIELD_VALUE);
263
+ if (in_trailer && (f == g_content_length ||
264
+ f == g_http_transfer_encoding || f == g_http_trailer))
265
+ parser_raise(eHttpParserError, "invalid Trailer");
266
+
246
267
  v = LEN(mark, p) == 0 ? rb_str_buf_new(128) : STRIPPED_STR_NEW(mark, p);
247
268
  if (NIL_P(f)) {
248
- const char *field = PTR_TO(start.field);
249
269
  size_t flen = hp->s.field_len;
250
270
 
251
271
  VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
@@ -254,14 +274,14 @@ static void write_value(VALUE self, struct http_parser *hp,
254
274
  * ignore "Version" headers since they conflict with the HTTP_VERSION
255
275
  * rack env variable.
256
276
  */
257
- if (CONST_MEM_EQ("VERSION", field, flen)) {
277
+ if (!in_trailer && CONST_MEM_EQ("VERSION", field, flen)) {
258
278
  RB_OBJ_WRITE(self, &hp->cont, Qnil);
259
279
  return;
260
280
  }
261
281
  f = uncommon_field(field, flen);
262
- } else if (f == g_http_connection) {
282
+ } else if (!in_trailer && f == g_http_connection) {
263
283
  hp_keepalive_connection(hp, v);
264
- } else if (f == g_content_length && !HP_FL_TEST(hp, CHUNKED)) {
284
+ } else if (!in_trailer && f == g_content_length && !HP_FL_TEST(hp, CHUNKED)) {
265
285
  if (hp->len.content)
266
286
  parser_raise(eHttpParserError, "Content-Length already set");
267
287
  hp->len.content = parse_length(RSTRING_PTR(v), RSTRING_LEN(v));
@@ -269,8 +289,7 @@ static void write_value(VALUE self, struct http_parser *hp,
269
289
  parser_raise(eHttpParserError, "invalid Content-Length");
270
290
  if (hp->len.content != 0)
271
291
  HP_FL_SET(hp, HASBODY);
272
- hp_invalid_if_trailer(hp);
273
- } else if (f == g_http_transfer_encoding) {
292
+ } else if (!in_trailer && f == g_http_transfer_encoding) {
274
293
  if (is_chunked(v)) {
275
294
  if (HP_FL_TEST(hp, CHUNKED))
276
295
  /*
@@ -296,19 +315,25 @@ static void write_value(VALUE self, struct http_parser *hp,
296
315
  */
297
316
  parser_raise(eHttpParserError, "invalid Transfer-Encoding");
298
317
  }
299
- hp_invalid_if_trailer(hp);
300
- } else if (f == g_http_trailer) {
318
+ } else if (!in_trailer && f == g_http_trailer) {
301
319
  HP_FL_SET(hp, HASTRAILER);
302
- hp_invalid_if_trailer(hp);
303
320
  } else {
304
321
  assert(TYPE(f) == T_STRING && "memoized object is not a string");
305
322
  assert_frozen(f);
306
323
  }
307
324
 
308
- e = rb_hash_aref(hp->env, f);
325
+ if (in_trailer) {
326
+ target = rb_hash_aref(hp->env, g_request_trailers);
327
+ if (NIL_P(target)) {
328
+ target = rb_hash_new();
329
+ rb_hash_aset(hp->env, g_request_trailers, target);
330
+ }
331
+ }
332
+
333
+ e = rb_hash_aref(target, f);
309
334
  if (NIL_P(e)) {
310
- RB_OBJ_WRITE(self, &hp->cont, rb_hash_aset(hp->env, f, v));
311
- } else if (f == g_http_host) {
335
+ RB_OBJ_WRITE(self, &hp->cont, rb_hash_aset(target, f, v));
336
+ } else if (!in_trailer && f == g_http_host) {
312
337
  /*
313
338
  * ignored, absolute URLs in REQUEST_URI take precedence over
314
339
  * the Host: header (ref: rfc 2616, section 5.2.1)
@@ -328,7 +353,7 @@ static void write_value(VALUE self, struct http_parser *hp,
328
353
  action mark {MARK(mark, fpc); }
329
354
 
330
355
  action start_field { MARK(start.field, fpc); }
331
- action snake_upcase_field { snake_upcase_char(deconst(fpc)); }
356
+ action upcase_field { upcase_char(deconst(fpc)); }
332
357
  action downcase_char { downcase_char(deconst(fpc)); }
333
358
  action write_field { hp->s.field_len = LEN(start.field, fpc); }
334
359
  action start_value { MARK(mark, fpc); }
@@ -398,6 +423,7 @@ static void write_value(VALUE self, struct http_parser *hp,
398
423
 
399
424
  action end_chunked_body {
400
425
  HP_FL_SET(hp, INTRAILER);
426
+ RB_OBJ_WRITE(self, &hp->cont, Qfalse);
401
427
  cs = http_parser_en_Trailers;
402
428
  ++p;
403
429
  assert(p <= pe && "buffer overflow after chunked body");
@@ -896,7 +922,8 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src)
896
922
  if (HP_FL_TEST(hp, CHUNKED)) {
897
923
  if (!chunked_eof(hp)) {
898
924
  rb_str_modify(dst);
899
- rb_str_resize(dst, srclen); /* we can never copy more than srclen bytes */
925
+ rb_str_set_len(dst, 0);
926
+ rb_str_modify_expand(dst, srclen); /* we can never copy more than srclen bytes */
900
927
 
901
928
  hp->s.dest_offset = 0;
902
929
  RB_OBJ_WRITE(self, &hp->cont, dst);
@@ -907,8 +934,12 @@ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src)
907
934
 
908
935
  assert(hp->s.dest_offset <= hp->offset &&
909
936
  "destination buffer overflow");
910
- advance_str(src, hp->offset);
937
+ /*
938
+ * set the decoded length before advance_str can allocate: GC
939
+ * compaction may re-embed dst and only copies its logical length
940
+ */
911
941
  rb_str_set_len(dst, hp->s.dest_offset);
942
+ advance_str(src, hp->offset);
912
943
 
913
944
  if (RSTRING_LEN(dst) == 0 && chunked_eof(hp)) {
914
945
  assert(hp->len.chunk == 0 && "chunk at EOF but more to parse");
@@ -49,7 +49,7 @@
49
49
  HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ;
50
50
  Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ;
51
51
 
52
- field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field;
52
+ field_name = ( token -- ":" )+ >start_field $upcase_field %write_field;
53
53
 
54
54
  field_value = content* >start_value %write_value;
55
55
 
@@ -4,12 +4,18 @@
4
4
  module Pitchfork
5
5
  # This class keep tracks of the state of all the monitor children.
6
6
  class Children
7
+ class << self
8
+ def load(data)
9
+ allocate.init_with(data)
10
+ end
11
+ end
12
+
7
13
  attr_reader :mold, :service
8
14
  attr_accessor :last_generation
9
15
 
10
16
  def initialize
11
17
  @last_generation = 0
12
- @children = {} # All children, including molds and services, indexed by PID.
18
+ @children = {} # All registered children, including molds and services, indexed by PID.
13
19
  @workers = {} # Workers indexed by their `nr`.
14
20
  @molds = {} # Molds, index by PID.
15
21
  @mold = nil # The latest mold, if any.
@@ -18,6 +24,43 @@ module Pitchfork
18
24
  @pending_molds = {} # Worker promoted to mold, not yet acknowledged
19
25
  end
20
26
 
27
+ def close_on_exec=(close_on_exec)
28
+ @children.each_value do |child|
29
+ child.close_on_exec = close_on_exec
30
+ end
31
+ end
32
+
33
+ def init_with(data)
34
+ @last_generation = 0
35
+
36
+ workers = data.map { |d| Worker.load(d) }
37
+ workers_with_pid, pending_workers = workers.partition(&:pid)
38
+ @children = workers_with_pid.to_h { |w| [w.pid, w] }
39
+ @workers = workers.select(&:worker?).to_h { |w| [w.nr, w] }
40
+ @pending_workers = pending_workers.select(&:worker?).to_h { |w| [w.nr, w] }
41
+
42
+ molds = workers_with_pid.select(&:mold?)
43
+ ready_molds, pending_molds = molds.partition(&:ready_mold?)
44
+ @molds = ready_molds.to_h { |m| [m.pid, m] }
45
+ @pending_molds = pending_molds.to_h { |m| [m.pid, m] }
46
+ @mold = ready_molds.max_by { |m| [m.version, m.generation] }
47
+
48
+ @service = workers.find(&:service?)
49
+
50
+ self
51
+ end
52
+
53
+ def dump
54
+ [
55
+ *@children.values,
56
+ *@workers.values,
57
+ *@molds.values,
58
+ @service,
59
+ *@pending_workers.values,
60
+ *@pending_molds.values
61
+ ].compact.uniq.map(&:dump)
62
+ end
63
+
21
64
  def register(child)
22
65
  # Children always start as workers, never molds, so we know they have a `#nr`.
23
66
  unless child.nr
@@ -54,21 +97,28 @@ module Pitchfork
54
97
  return service
55
98
  end
56
99
 
57
- child = @children[message.pid] || (message.nr && @workers[message.nr])
58
- child.update(message)
100
+ if child = @children[message.pid] || (message.nr && @workers[message.nr])
101
+ if child.pid && child.pid != message.pid
102
+ # An abandoned spawn may still come up late: never rebind a slot that already has a process.
103
+ return
104
+ end
59
105
 
60
- if child.mold?
61
- @pending_molds.delete(child.pid)
62
- @molds[child.pid] = child
63
- @mold = child
64
- end
106
+ child.update(message)
65
107
 
66
- if child.pid
67
- @children[child.pid] = child
68
- @pending_workers.delete(child.nr)
69
- end
108
+ if child.mold?
109
+ @pending_molds.delete(child.pid)
110
+ @molds[child.pid] = child
111
+ # Don't assume that new mold is the most up to date one.
112
+ @mold = @molds.values.max_by { |m| [m.version, m.generation] }
113
+ end
70
114
 
71
- child
115
+ if child.pid
116
+ @children[child.pid] = child
117
+ @pending_workers.delete(child.nr)
118
+ end
119
+
120
+ child
121
+ end
72
122
  end
73
123
 
74
124
  def nr_alive?(nr)
@@ -82,10 +132,10 @@ module Pitchfork
82
132
 
83
133
  def reap(pid)
84
134
  if child = @children.delete(pid)
85
- @pending_workers.delete(child.nr)
135
+ @pending_workers.delete(child.nr) if @pending_workers[child.nr].equal?(child)
86
136
  @pending_molds.delete(child.pid)
87
137
  @molds.delete(child.pid)
88
- @workers.delete(child.nr)
138
+ @workers.delete(child.nr) if @workers[child.nr]&.pid == child.pid
89
139
 
90
140
  if @mold == child
91
141
  @pending_workers.reject! do |nr, worker|
@@ -110,7 +160,9 @@ module Pitchfork
110
160
  end
111
161
 
112
162
  def restarting_workers_count
113
- @pending_workers.size + @workers.count { |_, w| w.exiting? }
163
+ @workers.count do |nr, worker|
164
+ @pending_workers.key?(nr) || worker.exiting? || !worker.ready?
165
+ end
114
166
  end
115
167
 
116
168
  def pending_promotion?
@@ -126,7 +178,7 @@ module Pitchfork
126
178
  end
127
179
 
128
180
  def each(&block)
129
- @children.each_value(&block)
181
+ (@children.values + @pending_workers.values).each(&block)
130
182
  end
131
183
 
132
184
  def each_worker(&block)
@@ -143,12 +195,14 @@ module Pitchfork
143
195
  child.hard_kill(sig)
144
196
  rescue Errno::ESRCH
145
197
  reap(child.pid)
146
- child.close
198
+ child.reaped!
147
199
  end
148
200
 
149
201
  def hard_kill_all(sig)
150
202
  each do |child|
151
- hard_kill(sig, child)
203
+ if child.pid
204
+ hard_kill(sig, child)
205
+ end
152
206
  end
153
207
  end
154
208
 
@@ -158,7 +212,7 @@ module Pitchfork
158
212
 
159
213
  def fresh_workers
160
214
  if @mold
161
- workers.select { |w| w.generation >= @mold.generation }
215
+ workers.reject(&:outdated?)
162
216
  else
163
217
  workers
164
218
  end
@@ -6,6 +6,8 @@ if defined?(Rack::RELEASE) && Rack::RELEASE > "3"
6
6
  require "rack/utils"
7
7
  end
8
8
 
9
+ require_relative "http_response"
10
+
9
11
  module Pitchfork
10
12
  # Middleware that applies chunked transfer encoding to response bodies
11
13
  # when the response does not include a content-length header.
@@ -77,6 +79,13 @@ module Pitchfork
77
79
  # Yield strings for each trailer header.
78
80
  def yield_trailers
79
81
  @body.trailers.each_pair do |k, v|
82
+ # Check bytes, not characters: obs-text is valid in field values
83
+ # even when the String's encoding says it is invalid.
84
+ k = k.to_s.b
85
+ v = v.to_s.b
86
+ next if HttpResponse::ILLEGAL_HEADER_VALUE.match?(k) || k.include?("\x7f")
87
+ next if HttpResponse::ILLEGAL_HEADER_VALUE.match?(v) || v.include?("\x7f")
88
+
80
89
  yield "#{k}: #{v}\r\n"
81
90
  end
82
91
  end
@@ -37,15 +37,12 @@ module Pitchfork
37
37
  :spawn_timeout => 10,
38
38
  :timeout_signal => -> (_pid) { :KILL },
39
39
  :timeout => 22,
40
+ :poller => :epoll_exclusive,
40
41
  :logger => default_logger,
41
42
  :worker_processes => 1,
42
43
  :before_fork => nil,
43
- :after_worker_fork => lambda { |server, worker|
44
- server.logger.info("#{worker.to_log} spawned")
45
- },
46
- :after_mold_fork => lambda { |server, worker|
47
- server.logger.info("#{worker.to_log} spawned")
48
- },
44
+ :after_worker_fork => nil,
45
+ :after_mold_fork => nil,
49
46
  :before_worker_exit => nil,
50
47
  :after_worker_exit => lambda { |server, worker, status|
51
48
  m = if worker.nil?
@@ -76,6 +73,7 @@ module Pitchfork
76
73
  :before_service_worker_ready => nil,
77
74
  :before_service_worker_exit => nil,
78
75
  :setpgid => true,
76
+ :working_directory => ENV["PWD"] || Dir.pwd
79
77
  }
80
78
  #:startdoc:
81
79
 
@@ -208,6 +206,15 @@ module Pitchfork
208
206
  set_bool(:setpgid, bool)
209
207
  end
210
208
 
209
+ POLLING_METHODS = %i(epoll_exclusive epoll select)
210
+
211
+ def poller(method)
212
+ unless POLLING_METHODS.include?(method)
213
+ raise ArgumentError, "poller must be one of: #{POLLING_METHODS.map(&:inspect).join(", ")}, got: #{method.inspect}"
214
+ end
215
+ set[:poller] = method
216
+ end
217
+
211
218
  def spawn_timeout(seconds)
212
219
  set_int(:spawn_timeout, seconds, 1)
213
220
  end
@@ -310,6 +317,18 @@ module Pitchfork
310
317
  end
311
318
  end
312
319
 
320
+ def working_directory(directory)
321
+ set[:working_directory] = directory
322
+ end
323
+
324
+ def restart_command(command)
325
+ set[:restart_command] = command
326
+ end
327
+
328
+ def restart_command_argv(restart_command_argv) # :nodoc:
329
+ set[:restart_command_argv] = restart_command_argv
330
+ end
331
+
313
332
  private
314
333
  def set_int(var, n, min) #:nodoc:
315
334
  Integer === n or raise ArgumentError, "not an integer: #{var}=#{n.inspect}"
@@ -24,23 +24,37 @@ module Pitchfork
24
24
  end
25
25
 
26
26
  def append_header(buf, key, value)
27
+ key = header_bytes(key)
27
28
  case value
28
29
  when Array # Rack 3
29
30
  value.each do |v|
31
+ v = header_bytes(v)
30
32
  next if ILLEGAL_HEADER_VALUE.match?(v)
31
- buf << "#{key}: #{v}\r\n"
32
- end
33
- when /\n/ # Rack 2
34
- # avoiding blank, key-only cookies with /\n+/
35
- value.split(/\n+/).each do |v|
36
- next if ILLEGAL_HEADER_VALUE.match?(v)
37
- buf << "#{key}: #{v}\r\n"
33
+ buf << key << ": " << v << "\r\n"
38
34
  end
39
35
  else
40
- buf << "#{key}: #{value}\r\n"
36
+ value = header_bytes(value)
37
+ if value.include?("\n") # Rack 2
38
+ # avoiding blank, key-only cookies with /\n+/
39
+ value.split(/\n+/).each do |v|
40
+ next if ILLEGAL_HEADER_VALUE.match?(v)
41
+ buf << key << ": " << v << "\r\n"
42
+ end
43
+ else
44
+ buf << key << ": " << value << "\r\n"
45
+ end
41
46
  end
42
47
  end
43
48
 
49
+ # Check and write bytes, not characters: obs-text (0x80-0xFF) is valid in
50
+ # field values even when the String's encoding says it is invalid, and
51
+ # the response buffer is binary. ASCII-only and binary Strings are used
52
+ # as they are to avoid a copy.
53
+ private def header_bytes(str)
54
+ str = str.to_s
55
+ str.ascii_only? || str.encoding == Encoding::BINARY ? str : str.b
56
+ end
57
+
44
58
  # writes the rack_response to socket as an HTTP response
45
59
  def http_response_write(socket, status, headers, body,
46
60
  req = Pitchfork::HttpParser.new)
@@ -54,10 +68,10 @@ module Pitchfork
54
68
  "Date: #{httpdate}\r\n" \
55
69
  "Connection: close\r\n".b
56
70
  headers.each do |key, value|
57
- case key
58
- when %r{\A(?:Date|Connection)\z}i
59
- next
60
- when "rack.hijack"
71
+ next if (key.bytesize == 4 && key.casecmp?("date")) ||
72
+ (key.bytesize == 10 && key.casecmp?("connection"))
73
+
74
+ if key == "rack.hijack"
61
75
  # This should only be hit under Rack >= 1.5, as this was an illegal
62
76
  # key in Rack < 1.5
63
77
  hijack = value
@@ -65,6 +79,15 @@ module Pitchfork
65
79
  append_header(buf, key, value)
66
80
  end
67
81
  end
82
+
83
+ # If the body is an array, we can directly send it with the headers in a single
84
+ # IO#write call.
85
+ # The vast majority of responses should qualify.
86
+ if !hijack && body.is_a?(Array) && body.size < 1000
87
+ socket.write(buf, "\r\n", *body)
88
+ return
89
+ end
90
+
68
91
  socket.write(buf << "\r\n")
69
92
  end
70
93