unicorn-rupcio 6.1.0

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.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +28 -0
  4. data/.gitattributes +5 -0
  5. data/.gitignore +25 -0
  6. data/.mailmap +26 -0
  7. data/.manifest +144 -0
  8. data/.olddoc.yml +25 -0
  9. data/Application_Timeouts +77 -0
  10. data/CONTRIBUTORS +39 -0
  11. data/COPYING +674 -0
  12. data/DESIGN +99 -0
  13. data/Documentation/.gitignore +3 -0
  14. data/Documentation/unicorn.1 +222 -0
  15. data/Documentation/unicorn_rails.1 +207 -0
  16. data/FAQ +70 -0
  17. data/GIT-VERSION-FILE +1 -0
  18. data/GIT-VERSION-GEN +39 -0
  19. data/GNUmakefile +318 -0
  20. data/HACKING +117 -0
  21. data/ISSUES +102 -0
  22. data/KNOWN_ISSUES +79 -0
  23. data/LICENSE +67 -0
  24. data/Links +58 -0
  25. data/PHILOSOPHY +139 -0
  26. data/README +165 -0
  27. data/Rakefile +17 -0
  28. data/SIGNALS +123 -0
  29. data/Sandbox +104 -0
  30. data/TODO +1 -0
  31. data/TUNING +119 -0
  32. data/archive/.gitignore +3 -0
  33. data/archive/slrnpull.conf +4 -0
  34. data/bin/unicorn +129 -0
  35. data/bin/unicorn_rails +210 -0
  36. data/examples/big_app_gc.rb +3 -0
  37. data/examples/echo.ru +27 -0
  38. data/examples/init.sh +102 -0
  39. data/examples/logger_mp_safe.rb +26 -0
  40. data/examples/logrotate.conf +44 -0
  41. data/examples/nginx.conf +156 -0
  42. data/examples/unicorn.conf.minimal.rb +14 -0
  43. data/examples/unicorn.conf.rb +111 -0
  44. data/examples/unicorn.socket +11 -0
  45. data/examples/unicorn@.service +40 -0
  46. data/ext/unicorn_http/CFLAGS +13 -0
  47. data/ext/unicorn_http/c_util.h +115 -0
  48. data/ext/unicorn_http/common_field_optimization.h +128 -0
  49. data/ext/unicorn_http/epollexclusive.h +128 -0
  50. data/ext/unicorn_http/ext_help.h +38 -0
  51. data/ext/unicorn_http/extconf.rb +40 -0
  52. data/ext/unicorn_http/global_variables.h +97 -0
  53. data/ext/unicorn_http/httpdate.c +91 -0
  54. data/ext/unicorn_http/unicorn_http.c +4348 -0
  55. data/ext/unicorn_http/unicorn_http.rl +1054 -0
  56. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  57. data/lib/unicorn/app/old_rails/static.rb +60 -0
  58. data/lib/unicorn/app/old_rails.rb +36 -0
  59. data/lib/unicorn/cgi_wrapper.rb +148 -0
  60. data/lib/unicorn/configurator.rb +749 -0
  61. data/lib/unicorn/const.rb +22 -0
  62. data/lib/unicorn/http_request.rb +180 -0
  63. data/lib/unicorn/http_response.rb +95 -0
  64. data/lib/unicorn/http_server.rb +860 -0
  65. data/lib/unicorn/launcher.rb +63 -0
  66. data/lib/unicorn/oob_gc.rb +82 -0
  67. data/lib/unicorn/preread_input.rb +34 -0
  68. data/lib/unicorn/select_waiter.rb +7 -0
  69. data/lib/unicorn/socket_helper.rb +186 -0
  70. data/lib/unicorn/stream_input.rb +152 -0
  71. data/lib/unicorn/tee_input.rb +132 -0
  72. data/lib/unicorn/tmpio.rb +34 -0
  73. data/lib/unicorn/util.rb +91 -0
  74. data/lib/unicorn/version.rb +1 -0
  75. data/lib/unicorn/worker.rb +166 -0
  76. data/lib/unicorn.rb +137 -0
  77. data/man/man1/unicorn.1 +222 -0
  78. data/man/man1/unicorn_rails.1 +207 -0
  79. data/setup.rb +1587 -0
  80. data/t/.gitignore +4 -0
  81. data/t/GNUmakefile +5 -0
  82. data/t/README +49 -0
  83. data/t/active-unix-socket.t +110 -0
  84. data/t/back-out-of-upgrade.t +44 -0
  85. data/t/bin/unused_listen +40 -0
  86. data/t/client_body_buffer_size.ru +15 -0
  87. data/t/client_body_buffer_size.t +79 -0
  88. data/t/detach.ru +12 -0
  89. data/t/env.ru +4 -0
  90. data/t/fails-rack-lint.ru +6 -0
  91. data/t/heartbeat-timeout.ru +13 -0
  92. data/t/heartbeat-timeout.t +60 -0
  93. data/t/integration.ru +129 -0
  94. data/t/integration.t +509 -0
  95. data/t/lib.perl +309 -0
  96. data/t/listener_names.ru +5 -0
  97. data/t/my-tap-lib.sh +201 -0
  98. data/t/oob_gc.ru +18 -0
  99. data/t/oob_gc_path.ru +18 -0
  100. data/t/pid.ru +4 -0
  101. data/t/preread_input.ru +23 -0
  102. data/t/reload-bad-config.t +49 -0
  103. data/t/reopen-logs.ru +14 -0
  104. data/t/reopen-logs.t +36 -0
  105. data/t/t0010-reap-logging.sh +55 -0
  106. data/t/t0012-reload-empty-config.sh +86 -0
  107. data/t/t0013-rewindable-input-false.sh +24 -0
  108. data/t/t0013.ru +13 -0
  109. data/t/t0014-rewindable-input-true.sh +24 -0
  110. data/t/t0014.ru +13 -0
  111. data/t/t0015-configurator-internals.sh +25 -0
  112. data/t/t0020-at_exit-handler.sh +49 -0
  113. data/t/t0021-process_detach.sh +29 -0
  114. data/t/t0022-listener_names-preload_app.sh +32 -0
  115. data/t/t0300-no-default-middleware.sh +20 -0
  116. data/t/t0301-no-default-middleware-ignored-in-config.sh +25 -0
  117. data/t/t0301.ru +14 -0
  118. data/t/t9001-oob_gc.sh +47 -0
  119. data/t/t9002-oob_gc-path.sh +75 -0
  120. data/t/test-lib.sh +125 -0
  121. data/t/winch_ttin.t +64 -0
  122. data/t/working_directory.t +86 -0
  123. data/test/aggregate.rb +16 -0
  124. data/test/benchmark/README +60 -0
  125. data/test/benchmark/dd.ru +19 -0
  126. data/test/benchmark/ddstream.ru +51 -0
  127. data/test/benchmark/readinput.ru +41 -0
  128. data/test/benchmark/stack.ru +9 -0
  129. data/test/benchmark/uconnect.perl +66 -0
  130. data/test/exec/README +5 -0
  131. data/test/exec/test_exec.rb +1030 -0
  132. data/test/test_helper.rb +307 -0
  133. data/test/unit/test_configurator.rb +176 -0
  134. data/test/unit/test_droplet.rb +29 -0
  135. data/test/unit/test_http_parser.rb +885 -0
  136. data/test/unit/test_http_parser_ng.rb +715 -0
  137. data/test/unit/test_server.rb +245 -0
  138. data/test/unit/test_signals.rb +189 -0
  139. data/test/unit/test_socket_helper.rb +160 -0
  140. data/test/unit/test_stream_input.rb +211 -0
  141. data/test/unit/test_tee_input.rb +304 -0
  142. data/test/unit/test_util.rb +132 -0
  143. data/test/unit/test_waiter.rb +35 -0
  144. data/unicorn.gemspec +49 -0
  145. metadata +266 -0
@@ -0,0 +1,156 @@
1
+ # This is example contains the bare mininum to get nginx going with
2
+ # unicorn servers. Generally these configuration settings
3
+ # are applicable to other HTTP application servers (and not just Ruby
4
+ # ones), so if you have one working well for proxying another app
5
+ # server, feel free to continue using it.
6
+ #
7
+ # The only setting we feel strongly about is the fail_timeout=0
8
+ # directive in the "upstream" block. max_fails=0 also has the same
9
+ # effect as fail_timeout=0 for current versions of nginx and may be
10
+ # used in its place.
11
+ #
12
+ # Users are strongly encouraged to refer to nginx documentation for more
13
+ # details and search for other example configs.
14
+
15
+ # you generally only need one nginx worker unless you're serving
16
+ # large amounts of static files which require blocking disk reads
17
+ worker_processes 1;
18
+
19
+ # # drop privileges, root is needed on most systems for binding to port 80
20
+ # # (or anything < 1024). Capability-based security may be available for
21
+ # # your system and worth checking out so you won't need to be root to
22
+ # # start nginx to bind on 80
23
+ user nobody nogroup; # for systems with a "nogroup"
24
+ # user nobody nobody; # for systems with "nobody" as a group instead
25
+
26
+ # Feel free to change all paths to suite your needs here, of course
27
+ pid /path/to/nginx.pid;
28
+ error_log /path/to/nginx.error.log;
29
+
30
+ events {
31
+ worker_connections 1024; # increase if you have lots of clients
32
+ accept_mutex off; # "on" if nginx worker_processes > 1
33
+ # use epoll; # enable for Linux 2.6+
34
+ # use kqueue; # enable for FreeBSD, OSX
35
+ }
36
+
37
+ http {
38
+ # nginx will find this file in the config directory set at nginx build time
39
+ include mime.types;
40
+
41
+ # fallback in case we can't determine a type
42
+ default_type application/octet-stream;
43
+
44
+ # click tracking!
45
+ access_log /path/to/nginx.access.log combined;
46
+
47
+ # you generally want to serve static files with nginx since
48
+ # unicorn is not and will never be optimized for it
49
+ sendfile on;
50
+
51
+ tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
52
+ tcp_nodelay off; # on may be better for some Comet/long-poll stuff
53
+
54
+ # we haven't checked to see if Rack::Deflate on the app server is
55
+ # faster or not than doing compression via nginx. It's easier
56
+ # to configure it all in one place here for static files and also
57
+ # to disable gzip for clients who don't get gzip/deflate right.
58
+ # There are other gzip settings that may be needed used to deal with
59
+ # bad clients out there, see
60
+ # https://nginx.org/en/docs/http/ngx_http_gzip_module.html
61
+ gzip on;
62
+ gzip_http_version 1.0;
63
+ gzip_proxied any;
64
+ gzip_min_length 500;
65
+ gzip_disable "MSIE [1-6]\.";
66
+ gzip_types text/plain text/html text/xml text/css
67
+ text/comma-separated-values
68
+ text/javascript application/x-javascript
69
+ application/atom+xml;
70
+
71
+ # this can be any application server, not just unicorn
72
+ upstream app_server {
73
+ # fail_timeout=0 means we always retry an upstream even if it failed
74
+ # to return a good HTTP response (in case the unicorn master nukes a
75
+ # single worker for timing out).
76
+
77
+ # for UNIX domain socket setups:
78
+ server unix:/path/to/.unicorn.sock fail_timeout=0;
79
+
80
+ # for TCP setups, point these to your backend servers
81
+ # server 192.168.0.7:8080 fail_timeout=0;
82
+ # server 192.168.0.8:8080 fail_timeout=0;
83
+ # server 192.168.0.9:8080 fail_timeout=0;
84
+ }
85
+
86
+ server {
87
+ # enable one of the following if you're on Linux or FreeBSD
88
+ # listen 80 default deferred; # for Linux
89
+ # listen 80 default accept_filter=httpready; # for FreeBSD
90
+
91
+ # If you have IPv6, you'll likely want to have two separate listeners.
92
+ # One on IPv4 only (the default), and another on IPv6 only instead
93
+ # of a single dual-stack listener. A dual-stack listener will make
94
+ # for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
95
+ # instead of just "10.0.0.1") and potentially trigger bugs in
96
+ # some software.
97
+ # listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
98
+
99
+ client_max_body_size 4G;
100
+ server_name _;
101
+
102
+ # ~2 seconds is often enough for most folks to parse HTML/CSS and
103
+ # retrieve needed images/icons/frames, connections are cheap in
104
+ # nginx so increasing this is generally safe...
105
+ keepalive_timeout 5;
106
+
107
+ # path for static files
108
+ root /path/to/app/current/public;
109
+
110
+ # Prefer to serve static files directly from nginx to avoid unnecessary
111
+ # data copies from the application server.
112
+ #
113
+ # try_files directive appeared in in nginx 0.7.27 and has stabilized
114
+ # over time. Older versions of nginx (e.g. 0.6.x) requires
115
+ # "if (!-f $request_filename)" which was less efficient:
116
+ # https://yhbt.net/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
117
+ try_files $uri/index.html $uri.html $uri @app;
118
+
119
+ location @app {
120
+ # an HTTP header important enough to have its own Wikipedia entry:
121
+ # https://en.wikipedia.org/wiki/X-Forwarded-For
122
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
123
+
124
+ # enable this if you forward HTTPS traffic to unicorn,
125
+ # this helps Rack set the proper URL scheme for doing redirects:
126
+ # proxy_set_header X-Forwarded-Proto $scheme;
127
+
128
+ # pass the Host: header from the client right along so redirects
129
+ # can be set properly within the Rack application
130
+ proxy_set_header Host $http_host;
131
+
132
+ # we don't want nginx trying to do something clever with
133
+ # redirects, we set the Host: header above already.
134
+ proxy_redirect off;
135
+
136
+ # It's also safe to set if you're using only serving fast clients
137
+ # with unicorn + nginx, but not slow clients. You normally want
138
+ # nginx to buffer responses to slow clients, even with Rails 3.1
139
+ # streaming because otherwise a slow client can become a bottleneck
140
+ # of unicorn.
141
+ #
142
+ # The Rack application may also set "X-Accel-Buffering (yes|no)"
143
+ # in the response headers do disable/enable buffering on a
144
+ # per-response basis.
145
+ # proxy_buffering off;
146
+
147
+ proxy_pass http://app_server;
148
+ }
149
+
150
+ # Rails error pages
151
+ error_page 500 502 503 504 /500.html;
152
+ location = /500.html {
153
+ root /path/to/app/current/public;
154
+ }
155
+ }
156
+ }
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+ # Minimal sample configuration file for Unicorn (not Rack) when used
3
+ # with daemonization (unicorn -D) started in your working directory.
4
+ #
5
+ # See https://yhbt.net/unicorn/Unicorn/Configurator.html for complete
6
+ # documentation.
7
+ # See also https://yhbt.net/unicorn/examples/unicorn.conf.rb for
8
+ # a more verbose configuration using more features.
9
+
10
+ listen 2007 # by default Unicorn listens on port 8080
11
+ worker_processes 2 # this should be >= nr_cpus
12
+ pid "/path/to/app/shared/pids/unicorn.pid"
13
+ stderr_path "/path/to/app/shared/log/unicorn.log"
14
+ stdout_path "/path/to/app/shared/log/unicorn.log"
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: false
2
+ # Sample verbose configuration file for Unicorn (not Rack)
3
+ #
4
+ # This configuration file documents many features of Unicorn
5
+ # that may not be needed for some applications. See
6
+ # https://yhbt.net/unicorn/examples/unicorn.conf.minimal.rb
7
+ # for a much simpler configuration file.
8
+ #
9
+ # See https://yhbt.net/unicorn/Unicorn/Configurator.html for complete
10
+ # documentation.
11
+
12
+ # Use at least one worker per core if you're on a dedicated server,
13
+ # more will usually help for _short_ waits on databases/caches.
14
+ worker_processes 4
15
+
16
+ # Since Unicorn is never exposed to outside clients, it does not need to
17
+ # run on the standard HTTP port (80), there is no reason to start Unicorn
18
+ # as root unless it's from system init scripts.
19
+ # If running the master process as root and the workers as an unprivileged
20
+ # user, do this to switch euid/egid in the workers (also chowns logs):
21
+ # user "unprivileged_user", "unprivileged_group"
22
+
23
+ # Help ensure your application will always spawn in the symlinked
24
+ # "current" directory that Capistrano sets up.
25
+ working_directory "/path/to/app/current" # available in 0.94.0+
26
+
27
+ # listen on both a Unix domain socket and a TCP port,
28
+ # we use a shorter backlog for quicker failover when busy
29
+ listen "/path/to/.unicorn.sock", :backlog => 64
30
+ listen 8080, :tcp_nopush => true
31
+
32
+ # nuke workers after 30 seconds instead of 60 seconds (the default)
33
+ timeout 30
34
+
35
+ # feel free to point this anywhere accessible on the filesystem
36
+ pid "/path/to/app/shared/pids/unicorn.pid"
37
+
38
+ # By default, the Unicorn logger will write to stderr.
39
+ # Additionally, ome applications/frameworks log to stderr or stdout,
40
+ # so prevent them from going to /dev/null when daemonized here:
41
+ stderr_path "/path/to/app/shared/log/unicorn.stderr.log"
42
+ stdout_path "/path/to/app/shared/log/unicorn.stdout.log"
43
+
44
+ # combine Ruby 2.0.0+ with "preload_app true" for memory savings
45
+ preload_app true
46
+
47
+ # Enable this flag to have unicorn test client connections by writing the
48
+ # beginning of the HTTP headers before calling the application. This
49
+ # prevents calling the application for connections that have disconnected
50
+ # while queued. This is only guaranteed to detect clients on the same
51
+ # host unicorn runs on, and unlikely to detect disconnects even on a
52
+ # fast LAN.
53
+ check_client_connection false
54
+
55
+ # local variable to guard against running a hook multiple times
56
+ run_once = true
57
+
58
+ before_fork do |server, worker|
59
+ # the following is highly recomended for Rails + "preload_app true"
60
+ # as there's no need for the master process to hold a connection
61
+ defined?(ActiveRecord::Base) and
62
+ ActiveRecord::Base.connection.disconnect!
63
+
64
+ # Occasionally, it may be necessary to run non-idempotent code in the
65
+ # master before forking. Keep in mind the above disconnect! example
66
+ # is idempotent and does not need a guard.
67
+ if run_once
68
+ # do_something_once_here ...
69
+ run_once = false # prevent from firing again
70
+ end
71
+
72
+ # The following is only recommended for memory/DB-constrained
73
+ # installations. It is not needed if your system can house
74
+ # twice as many worker_processes as you have configured.
75
+ #
76
+ # # This allows a new master process to incrementally
77
+ # # phase out the old master process with SIGTTOU to avoid a
78
+ # # thundering herd (especially in the "preload_app false" case)
79
+ # # when doing a transparent upgrade. The last worker spawned
80
+ # # will then kill off the old master process with a SIGQUIT.
81
+ # old_pid = "#{server.config[:pid]}.oldbin"
82
+ # if old_pid != server.pid
83
+ # begin
84
+ # sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
85
+ # Process.kill(sig, File.read(old_pid).to_i)
86
+ # rescue Errno::ENOENT, Errno::ESRCH
87
+ # end
88
+ # end
89
+ #
90
+ # Throttle the master from forking too quickly by sleeping. Due
91
+ # to the implementation of standard Unix signal handlers, this
92
+ # helps (but does not completely) prevent identical, repeated signals
93
+ # from being lost when the receiving process is busy.
94
+ # sleep 1
95
+ end
96
+
97
+ after_fork do |server, worker|
98
+ # per-process listener ports for debugging/admin/migrations
99
+ # addr = "127.0.0.1:#{9293 + worker.nr}"
100
+ # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
101
+
102
+ # the following is *required* for Rails + "preload_app true",
103
+ defined?(ActiveRecord::Base) and
104
+ ActiveRecord::Base.establish_connection
105
+
106
+ # if preload_app is true, then you may also want to check and
107
+ # restart any other shared sockets/descriptors such as Memcached,
108
+ # and Redis. TokyoCabinet file handles are safe to reuse
109
+ # between any number of forked children (assuming your kernel
110
+ # correctly implements pread()/pwrite() system calls)
111
+ end
@@ -0,0 +1,11 @@
1
+ # ==> /etc/systemd/system/unicorn.socket <==
2
+ [Unit]
3
+ Description = unicorn sockets
4
+
5
+ [Socket]
6
+ ListenStream = 127.0.0.1:8080
7
+ ListenStream = /tmp/path/to/.unicorn.sock
8
+ Service = unicorn@1.service
9
+
10
+ [Install]
11
+ WantedBy = sockets.target
@@ -0,0 +1,40 @@
1
+ # ==> /etc/systemd/system/unicorn@.service <==
2
+ # Since SIGUSR2 upgrades do not work under systemd, this service file
3
+ # allows starting two simultaneous services during upgrade time
4
+ # (e.g. unicorn@1 unicorn@2) with the intention that they take
5
+ # turns running in-between upgrades. This should allow upgrading
6
+ # without downtime.
7
+
8
+ [Unit]
9
+ Description = unicorn Rack application server %i
10
+ Wants = unicorn.socket
11
+ After = unicorn.socket
12
+
13
+ [Service]
14
+ # bundler users must use the "--keep-file-descriptors" switch, here:
15
+ # ExecStart = bundle exec --keep-file-descriptors unicorn -c ...
16
+ ExecStart = /usr/bin/unicorn -c /path/to/unicorn.conf.rb /path/to/config.ru
17
+
18
+ # NonBlocking MUST be true if using socket activation with unicorn.
19
+ # Otherwise, there's a small window in-between when the non-blocking
20
+ # flag is set by us and our accept4 call where systemd can momentarily
21
+ # make the socket blocking, causing us to block on accept4:
22
+ NonBlocking = true
23
+ Sockets = unicorn.socket
24
+
25
+ KillSignal = SIGQUIT
26
+ User = nobody
27
+ Group = nogroup
28
+ ExecReload = /bin/kill -HUP $MAINPID
29
+
30
+ # This is based on the Unicorn::Configurator#timeout directive,
31
+ # adding a few seconds for scheduling differences:
32
+ TimeoutStopSec = 62
33
+
34
+ # Only kill the master process, it may be harmful to signal
35
+ # workers via default "control-group" setting since some
36
+ # Ruby extensions and applications misbehave on interrupts
37
+ KillMode = process
38
+
39
+ [Install]
40
+ WantedBy = multi-user.target
@@ -0,0 +1,13 @@
1
+ # CFLAGS used for development (gcc-dependent)
2
+ # source this file if you want/need them
3
+ CFLAGS=
4
+ CFLAGS="$CFLAGS -Wall"
5
+ CFLAGS="$CFLAGS -Wwrite-strings"
6
+ CFLAGS="$CFLAGS -Wdeclaration-after-statement"
7
+ CFLAGS="$CFLAGS -Wcast-qual"
8
+ CFLAGS="$CFLAGS -Wstrict-prototypes"
9
+ CFLAGS="$CFLAGS -Wshadow"
10
+ CFLAGS="$CFLAGS -Wextra"
11
+ CFLAGS="$CFLAGS -Wno-deprecated-declarations"
12
+ CFLAGS="$CFLAGS -Waggregate-return"
13
+ CFLAGS="$CFLAGS -Wchar-subscripts"
@@ -0,0 +1,115 @@
1
+ /*
2
+ * Generic C functions and macros go here, there are no dependencies
3
+ * on Unicorn internal structures or the Ruby C API in here.
4
+ */
5
+
6
+ #ifndef UH_util_h
7
+ #define UH_util_h
8
+
9
+ #include <unistd.h>
10
+ #include <assert.h>
11
+ #include <limits.h>
12
+
13
+ #define MIN(a,b) (a < b ? a : b)
14
+ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
15
+
16
+ #if SIZEOF_OFF_T == SIZEOF_INT
17
+ # define UH_OFF_T_MAX INT_MAX
18
+ #elif SIZEOF_OFF_T == SIZEOF_LONG_LONG
19
+ # define UH_OFF_T_MAX LLONG_MAX
20
+ #else
21
+ # error off_t size unknown for this platform!
22
+ #endif /* SIZEOF_OFF_T check */
23
+
24
+ /*
25
+ * ragel enforces fpc as a const, and merely casting can make picky
26
+ * compilers unhappy, so we have this little helper do our dirty work
27
+ */
28
+ static inline void *deconst(const void *in)
29
+ {
30
+ union { const void *in; void *out; } tmp;
31
+
32
+ tmp.in = in;
33
+
34
+ return tmp.out;
35
+ }
36
+
37
+ /*
38
+ * Capitalizes all lower-case ASCII characters, locale-agnostic.
39
+ * We don't .tr('-', '_') here since we need to ban /^content_length$/i
40
+ * and /^transfer_encoding$/i to avoid confusion and smuggling attacks.
41
+ */
42
+ static void upcase_char(char *c)
43
+ {
44
+ if (*c >= 'a' && *c <= 'z')
45
+ *c &= ~0x20;
46
+ }
47
+
48
+ /* Downcases a single ASCII character. Locale-agnostic. */
49
+ static void downcase_char(char *c)
50
+ {
51
+ if (*c >= 'A' && *c <= 'Z')
52
+ *c |= 0x20;
53
+ }
54
+
55
+ static int hexchar2int(int xdigit)
56
+ {
57
+ if (xdigit >= 'A' && xdigit <= 'F')
58
+ return xdigit - 'A' + 10;
59
+ if (xdigit >= 'a' && xdigit <= 'f')
60
+ return xdigit - 'a' + 10;
61
+
62
+ /* Ragel already does runtime range checking for us in Unicorn: */
63
+ assert(xdigit >= '0' && xdigit <= '9' && "invalid digit character");
64
+
65
+ return xdigit - '0';
66
+ }
67
+
68
+ /*
69
+ * multiplies +i+ by +base+ and increments the result by the parsed
70
+ * integer value of +xdigit+. +xdigit+ is a character byte
71
+ * representing a number the range of 0..(base-1)
72
+ * returns the new value of +i+ on success
73
+ * returns -1 on errors (including overflow)
74
+ */
75
+ static off_t step_incr(off_t i, int xdigit, const int base)
76
+ {
77
+ static const off_t max = UH_OFF_T_MAX;
78
+ const off_t next_max = (max - (max % base)) / base;
79
+ off_t offset = hexchar2int(xdigit);
80
+
81
+ if (offset > (base - 1))
82
+ return -1;
83
+ if (i > next_max)
84
+ return -1;
85
+ i *= base;
86
+
87
+ if ((offset > (base - 1)) || ((max - i) < offset))
88
+ return -1;
89
+
90
+ return i + offset;
91
+ }
92
+
93
+ /*
94
+ * parses a non-negative length according to base-10 and
95
+ * returns it as an off_t value. Returns -1 on errors
96
+ * (including overflow).
97
+ */
98
+ static off_t parse_length(const char *value, size_t length)
99
+ {
100
+ off_t rv;
101
+
102
+ for (rv = 0; length-- && rv >= 0; ++value) {
103
+ if (*value >= '0' && *value <= '9')
104
+ rv = step_incr(rv, *value, 10);
105
+ else
106
+ return -1;
107
+ }
108
+
109
+ return rv;
110
+ }
111
+
112
+ #define CONST_MEM_EQ(const_p, buf, len) \
113
+ ((sizeof(const_p) - 1) == len && !memcmp(const_p, buf, sizeof(const_p) - 1))
114
+
115
+ #endif /* UH_util_h */
@@ -0,0 +1,128 @@
1
+ #ifndef common_field_optimization
2
+ #define common_field_optimization
3
+ #include "ruby.h"
4
+ #include "c_util.h"
5
+
6
+ struct common_field {
7
+ const signed long len;
8
+ const char *name;
9
+ VALUE value;
10
+ };
11
+
12
+ /*
13
+ * A list of common HTTP headers we expect to receive.
14
+ * This allows us to avoid repeatedly creating identical string
15
+ * objects to be used with rb_hash_aset().
16
+ */
17
+ static struct common_field common_http_fields[] = {
18
+ # define f(N) { (sizeof(N) - 1), N, Qnil }
19
+ f("ACCEPT"),
20
+ f("ACCEPT_CHARSET"),
21
+ f("ACCEPT_ENCODING"),
22
+ f("ACCEPT_LANGUAGE"),
23
+ f("ALLOW"),
24
+ f("AUTHORIZATION"),
25
+ f("CACHE_CONTROL"),
26
+ f("CONNECTION"),
27
+ f("CONTENT_ENCODING"),
28
+ f("CONTENT_LENGTH"),
29
+ f("CONTENT_TYPE"),
30
+ f("COOKIE"),
31
+ f("DATE"),
32
+ f("EXPECT"),
33
+ f("FROM"),
34
+ f("HOST"),
35
+ f("IF_MATCH"),
36
+ f("IF_MODIFIED_SINCE"),
37
+ f("IF_NONE_MATCH"),
38
+ f("IF_RANGE"),
39
+ f("IF_UNMODIFIED_SINCE"),
40
+ f("KEEP_ALIVE"), /* Firefox sends this */
41
+ f("MAX_FORWARDS"),
42
+ f("PRAGMA"),
43
+ f("PROXY_AUTHORIZATION"),
44
+ f("RANGE"),
45
+ f("REFERER"),
46
+ f("TE"),
47
+ f("TRAILER"),
48
+ f("TRANSFER_ENCODING"),
49
+ f("UPGRADE"),
50
+ f("USER_AGENT"),
51
+ f("VIA"),
52
+ f("X_FORWARDED_FOR"), /* common for proxies */
53
+ f("X_FORWARDED_PROTO"), /* common for proxies */
54
+ f("X_REAL_IP"), /* common for proxies */
55
+ f("WARNING")
56
+ # undef f
57
+ };
58
+
59
+ #define HTTP_PREFIX "HTTP_"
60
+ #define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1)
61
+ static ID id_uminus;
62
+
63
+ /* this dedupes under Ruby 2.5+ (December 2017) */
64
+ static VALUE str_dd_freeze(VALUE str)
65
+ {
66
+ if (STR_UMINUS_DEDUPE)
67
+ return rb_funcall(str, id_uminus, 0);
68
+
69
+ /* freeze,since it speeds up older MRI slightly */
70
+ OBJ_FREEZE(str);
71
+ return str;
72
+ }
73
+
74
+ static VALUE str_new_dd_freeze(const char *ptr, long len)
75
+ {
76
+ return str_dd_freeze(rb_str_new(ptr, len));
77
+ }
78
+
79
+ /* this function is not performance-critical, called only at load time */
80
+ static void init_common_fields(void)
81
+ {
82
+ int i;
83
+ struct common_field *cf = common_http_fields;
84
+ char tmp[64];
85
+
86
+ memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN);
87
+
88
+ for(i = ARRAY_SIZE(common_http_fields); --i >= 0; cf++) {
89
+ /* Rack doesn't like certain headers prefixed with "HTTP_" */
90
+ if (!strcmp("CONTENT_LENGTH", cf->name) ||
91
+ !strcmp("CONTENT_TYPE", cf->name)) {
92
+ cf->value = str_new_dd_freeze(cf->name, cf->len);
93
+ } else {
94
+ memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
95
+ cf->value = str_new_dd_freeze(tmp, HTTP_PREFIX_LEN + cf->len);
96
+ }
97
+ rb_gc_register_mark_object(cf->value);
98
+ }
99
+ }
100
+
101
+ /* this function is called for every header set */
102
+ static VALUE find_common_field(const char *field, size_t flen)
103
+ {
104
+ int i;
105
+ struct common_field *cf = common_http_fields;
106
+
107
+ for(i = ARRAY_SIZE(common_http_fields); --i >= 0; cf++) {
108
+ if (cf->len == (long)flen && !memcmp(cf->name, field, flen))
109
+ return cf->value;
110
+ }
111
+ return Qnil;
112
+ }
113
+
114
+ /*
115
+ * We got a strange header that we don't have a memoized value for.
116
+ * Fallback to creating a new string to use as a hash key.
117
+ */
118
+ static VALUE uncommon_field(const char *field, size_t flen)
119
+ {
120
+ VALUE f = rb_str_new(NULL, HTTP_PREFIX_LEN + flen);
121
+ memcpy(RSTRING_PTR(f), HTTP_PREFIX, HTTP_PREFIX_LEN);
122
+ memcpy(RSTRING_PTR(f) + HTTP_PREFIX_LEN, field, flen);
123
+ assert(*(RSTRING_PTR(f) + RSTRING_LEN(f)) == '\0' &&
124
+ "string didn't end with \\0"); /* paranoia */
125
+ return HASH_ASET_DEDUPE ? f : str_dd_freeze(f);
126
+ }
127
+
128
+ #endif /* common_field_optimization_h */