unicorn-shopify 4.8.2.5.23

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 (150) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +28 -0
  4. data/.gitignore +25 -0
  5. data/.mailmap +26 -0
  6. data/.olddoc.yml +15 -0
  7. data/Application_Timeouts +77 -0
  8. data/CONTRIBUTORS +35 -0
  9. data/COPYING +674 -0
  10. data/DESIGN +97 -0
  11. data/Documentation/.gitignore +5 -0
  12. data/Documentation/GNUmakefile +30 -0
  13. data/Documentation/unicorn.1.txt +185 -0
  14. data/Documentation/unicorn_rails.1.txt +175 -0
  15. data/FAQ +61 -0
  16. data/GIT-VERSION-GEN +39 -0
  17. data/GNUmakefile +252 -0
  18. data/HACKING +120 -0
  19. data/ISSUES +100 -0
  20. data/KNOWN_ISSUES +79 -0
  21. data/LICENSE +67 -0
  22. data/Links +59 -0
  23. data/PHILOSOPHY +145 -0
  24. data/README +145 -0
  25. data/Rakefile +16 -0
  26. data/SIGNALS +123 -0
  27. data/Sandbox +103 -0
  28. data/TODO +5 -0
  29. data/TUNING +101 -0
  30. data/archive/.gitignore +3 -0
  31. data/archive/slrnpull.conf +4 -0
  32. data/bin/unicorn +126 -0
  33. data/bin/unicorn_rails +209 -0
  34. data/examples/big_app_gc.rb +2 -0
  35. data/examples/echo.ru +27 -0
  36. data/examples/init.sh +74 -0
  37. data/examples/logger_mp_safe.rb +25 -0
  38. data/examples/logrotate.conf +29 -0
  39. data/examples/nginx.conf +156 -0
  40. data/examples/unicorn.conf.minimal.rb +13 -0
  41. data/examples/unicorn.conf.rb +113 -0
  42. data/ext/unicorn_http/CFLAGS +13 -0
  43. data/ext/unicorn_http/c_util.h +124 -0
  44. data/ext/unicorn_http/common_field_optimization.h +111 -0
  45. data/ext/unicorn_http/ext_help.h +82 -0
  46. data/ext/unicorn_http/extconf.rb +10 -0
  47. data/ext/unicorn_http/global_variables.h +97 -0
  48. data/ext/unicorn_http/httpdate.c +78 -0
  49. data/ext/unicorn_http/unicorn_http.rl +934 -0
  50. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  51. data/lib/unicorn.rb +112 -0
  52. data/lib/unicorn/app/old_rails.rb +35 -0
  53. data/lib/unicorn/app/old_rails/static.rb +59 -0
  54. data/lib/unicorn/cgi_wrapper.rb +147 -0
  55. data/lib/unicorn/configurator.rb +686 -0
  56. data/lib/unicorn/const.rb +21 -0
  57. data/lib/unicorn/http_request.rb +125 -0
  58. data/lib/unicorn/http_response.rb +73 -0
  59. data/lib/unicorn/http_server.rb +816 -0
  60. data/lib/unicorn/launcher.rb +62 -0
  61. data/lib/unicorn/oob_gc.rb +81 -0
  62. data/lib/unicorn/preread_input.rb +33 -0
  63. data/lib/unicorn/socket_helper.rb +197 -0
  64. data/lib/unicorn/stream_input.rb +146 -0
  65. data/lib/unicorn/tee_input.rb +133 -0
  66. data/lib/unicorn/tmpio.rb +27 -0
  67. data/lib/unicorn/util.rb +90 -0
  68. data/lib/unicorn/worker.rb +140 -0
  69. data/setup.rb +1586 -0
  70. data/t/.gitignore +4 -0
  71. data/t/GNUmakefile +74 -0
  72. data/t/README +42 -0
  73. data/t/before_murder.ru +7 -0
  74. data/t/bin/content-md5-put +36 -0
  75. data/t/bin/sha1sum.rb +17 -0
  76. data/t/bin/unused_listen +40 -0
  77. data/t/broken-app.ru +12 -0
  78. data/t/detach.ru +11 -0
  79. data/t/env.ru +3 -0
  80. data/t/fails-rack-lint.ru +5 -0
  81. data/t/heartbeat-timeout.ru +12 -0
  82. data/t/hijack.ru +42 -0
  83. data/t/listener_names.ru +4 -0
  84. data/t/my-tap-lib.sh +201 -0
  85. data/t/oob_gc.ru +20 -0
  86. data/t/oob_gc_path.ru +20 -0
  87. data/t/pid.ru +3 -0
  88. data/t/preread_input.ru +17 -0
  89. data/t/rack-input-tests.ru +21 -0
  90. data/t/t0000-http-basic.sh +50 -0
  91. data/t/t0001-reload-bad-config.sh +53 -0
  92. data/t/t0002-config-conflict.sh +49 -0
  93. data/t/t0002-parser-error.sh +94 -0
  94. data/t/t0003-working_directory.sh +51 -0
  95. data/t/t0004-heartbeat-timeout.sh +69 -0
  96. data/t/t0004-working_directory_broken.sh +24 -0
  97. data/t/t0005-working_directory_app.rb.sh +40 -0
  98. data/t/t0006-reopen-logs.sh +83 -0
  99. data/t/t0006.ru +13 -0
  100. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  101. data/t/t0008-back_out_of_upgrade.sh +110 -0
  102. data/t/t0009-broken-app.sh +56 -0
  103. data/t/t0009-winch_ttin.sh +59 -0
  104. data/t/t0010-reap-logging.sh +55 -0
  105. data/t/t0011-active-unix-socket.sh +79 -0
  106. data/t/t0012-reload-empty-config.sh +85 -0
  107. data/t/t0013-rewindable-input-false.sh +24 -0
  108. data/t/t0013.ru +12 -0
  109. data/t/t0014-rewindable-input-true.sh +24 -0
  110. data/t/t0014.ru +12 -0
  111. data/t/t0015-configurator-internals.sh +25 -0
  112. data/t/t0018-write-on-close.sh +23 -0
  113. data/t/t0019-max_header_len.sh +49 -0
  114. data/t/t0020-at_exit-handler.sh +49 -0
  115. data/t/t0021-process_detach.sh +29 -0
  116. data/t/t0022-listener_names-preload_app.sh +32 -0
  117. data/t/t0023-before-murder.sh +40 -0
  118. data/t/t0024-before-murder_once.sh +52 -0
  119. data/t/t0100-rack-input-tests.sh +124 -0
  120. data/t/t0116-client_body_buffer_size.sh +80 -0
  121. data/t/t0116.ru +16 -0
  122. data/t/t0200-rack-hijack.sh +27 -0
  123. data/t/t0300-no-default-middleware.sh +20 -0
  124. data/t/t9000-preread-input.sh +48 -0
  125. data/t/t9001-oob_gc.sh +47 -0
  126. data/t/t9002-oob_gc-path.sh +75 -0
  127. data/t/test-lib.sh +128 -0
  128. data/t/write-on-close.ru +11 -0
  129. data/test/aggregate.rb +15 -0
  130. data/test/benchmark/README +50 -0
  131. data/test/benchmark/dd.ru +18 -0
  132. data/test/benchmark/stack.ru +8 -0
  133. data/test/exec/README +5 -0
  134. data/test/exec/test_exec.rb +1047 -0
  135. data/test/test_helper.rb +297 -0
  136. data/test/unit/test_configurator.rb +175 -0
  137. data/test/unit/test_droplet.rb +28 -0
  138. data/test/unit/test_http_parser.rb +854 -0
  139. data/test/unit/test_http_parser_ng.rb +622 -0
  140. data/test/unit/test_request.rb +182 -0
  141. data/test/unit/test_response.rb +93 -0
  142. data/test/unit/test_server.rb +268 -0
  143. data/test/unit/test_signals.rb +188 -0
  144. data/test/unit/test_socket_helper.rb +197 -0
  145. data/test/unit/test_stream_input.rb +203 -0
  146. data/test/unit/test_tee_input.rb +304 -0
  147. data/test/unit/test_upload.rb +306 -0
  148. data/test/unit/test_util.rb +105 -0
  149. data/unicorn.gemspec +41 -0
  150. metadata +311 -0
@@ -0,0 +1,25 @@
1
+ # Multi-Processing-safe monkey patch for Logger
2
+ #
3
+ # This monkey patch fixes the case where "preload_app true" is used and
4
+ # the application spawns a background thread upon being loaded.
5
+ #
6
+ # This removes all lock from the Logger code and solely relies on the
7
+ # underlying filesystem to handle write(2) system calls atomically when
8
+ # O_APPEND is used. This is safe in the presence of both multiple
9
+ # threads (native or green) and multiple processes when writing to
10
+ # a filesystem with POSIX O_APPEND semantics.
11
+ #
12
+ # It should be noted that the original locking on Logger could _never_ be
13
+ # considered reliable on non-POSIX filesystems with multiple processes,
14
+ # either, so nothing is lost in that case.
15
+
16
+ require 'logger'
17
+ class Logger::LogDevice
18
+ def write(message)
19
+ @dev.syswrite(message)
20
+ end
21
+
22
+ def close
23
+ @dev.close
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ # example logrotate config file, I usually keep this in
2
+ # /etc/logrotate.d/unicorn_app on my Debian systems
3
+ #
4
+ # See the logrotate(8) manpage for more information:
5
+ # http://linux.die.net/man/8/logrotate
6
+
7
+ # Modify the following glob to match the logfiles your app writes to:
8
+ /var/log/unicorn_app/*.log {
9
+ # this first block is mostly just personal preference, though
10
+ # I wish logrotate offered an "hourly" option...
11
+ daily
12
+ missingok
13
+ rotate 180
14
+ compress # must use with delaycompress below
15
+ dateext
16
+
17
+ # this is important if using "compress" since we need to call
18
+ # the "lastaction" script below before compressing:
19
+ delaycompress
20
+
21
+ # note the lack of the evil "copytruncate" option in this
22
+ # config. Unicorn supports the USR1 signal and we send it
23
+ # as our "lastaction" action:
24
+ lastaction
25
+ # assuming your pid file is in /var/run/unicorn_app/pid
26
+ pid=/var/run/unicorn_app/pid
27
+ test -s $pid && kill -USR1 "$(cat $pid)"
28
+ endscript
29
+ }
@@ -0,0 +1,156 @@
1
+ # This is example contains the bare mininum to get nginx going with
2
+ # Unicorn or Rainbows! 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 neither
48
+ # Unicorn nor Rainbows! is optimized for it at the moment
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 http://wiki.nginx.org/NginxHttpGzipModule
60
+ gzip on;
61
+ gzip_http_version 1.0;
62
+ gzip_proxied any;
63
+ gzip_min_length 500;
64
+ gzip_disable "MSIE [1-6]\.";
65
+ gzip_types text/plain text/html text/xml text/css
66
+ text/comma-separated-values
67
+ text/javascript application/x-javascript
68
+ application/atom+xml;
69
+
70
+ # this can be any application server, not just Unicorn/Rainbows!
71
+ upstream app_server {
72
+ # fail_timeout=0 means we always retry an upstream even if it failed
73
+ # to return a good HTTP response (in case the Unicorn master nukes a
74
+ # single worker for timing out).
75
+
76
+ # for UNIX domain socket setups:
77
+ server unix:/path/to/.unicorn.sock fail_timeout=0;
78
+
79
+ # for TCP setups, point these to your backend servers
80
+ # server 192.168.0.7:8080 fail_timeout=0;
81
+ # server 192.168.0.8:8080 fail_timeout=0;
82
+ # server 192.168.0.9:8080 fail_timeout=0;
83
+ }
84
+
85
+ server {
86
+ # enable one of the following if you're on Linux or FreeBSD
87
+ # listen 80 default deferred; # for Linux
88
+ # listen 80 default accept_filter=httpready; # for FreeBSD
89
+
90
+ # If you have IPv6, you'll likely want to have two separate listeners.
91
+ # One on IPv4 only (the default), and another on IPv6 only instead
92
+ # of a single dual-stack listener. A dual-stack listener will make
93
+ # for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
94
+ # instead of just "10.0.0.1") and potentially trigger bugs in
95
+ # some software.
96
+ # listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
97
+
98
+ client_max_body_size 4G;
99
+ server_name _;
100
+
101
+ # ~2 seconds is often enough for most folks to parse HTML/CSS and
102
+ # retrieve needed images/icons/frames, connections are cheap in
103
+ # nginx so increasing this is generally safe...
104
+ keepalive_timeout 5;
105
+
106
+ # path for static files
107
+ root /path/to/app/current/public;
108
+
109
+ # Prefer to serve static files directly from nginx to avoid unnecessary
110
+ # data copies from the application server.
111
+ #
112
+ # try_files directive appeared in in nginx 0.7.27 and has stabilized
113
+ # over time. Older versions of nginx (e.g. 0.6.x) requires
114
+ # "if (!-f $request_filename)" which was less efficient:
115
+ # http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
116
+ try_files $uri/index.html $uri.html $uri @app;
117
+
118
+ location @app {
119
+ # an HTTP header important enough to have its own Wikipedia entry:
120
+ # http://en.wikipedia.org/wiki/X-Forwarded-For
121
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
122
+
123
+ # enable this if you forward HTTPS traffic to unicorn,
124
+ # this helps Rack set the proper URL scheme for doing redirects:
125
+ # proxy_set_header X-Forwarded-Proto $scheme;
126
+
127
+ # pass the Host: header from the client right along so redirects
128
+ # can be set properly within the Rack application
129
+ proxy_set_header Host $http_host;
130
+
131
+ # we don't want nginx trying to do something clever with
132
+ # redirects, we set the Host: header above already.
133
+ proxy_redirect off;
134
+
135
+ # set "proxy_buffering off" *only* for Rainbows! when doing
136
+ # Comet/long-poll/streaming. It's also safe to set if you're using
137
+ # only serving fast clients with Unicorn + nginx, but not slow
138
+ # clients. You normally want nginx to buffer responses to slow
139
+ # clients, even with Rails 3.1 streaming because otherwise a slow
140
+ # client can become a bottleneck 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,13 @@
1
+ # Minimal sample configuration file for Unicorn (not Rack) when used
2
+ # with daemonization (unicorn -D) started in your working directory.
3
+ #
4
+ # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
5
+ # documentation.
6
+ # See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for
7
+ # a more verbose configuration using more features.
8
+
9
+ listen 2007 # by default Unicorn listens on port 8080
10
+ worker_processes 2 # this should be >= nr_cpus
11
+ pid "/path/to/app/shared/pids/unicorn.pid"
12
+ stderr_path "/path/to/app/shared/log/unicorn.log"
13
+ stdout_path "/path/to/app/shared/log/unicorn.log"
@@ -0,0 +1,113 @@
1
+ # Sample verbose configuration file for Unicorn (not Rack)
2
+ #
3
+ # This configuration file documents many features of Unicorn
4
+ # that may not be needed for some applications. See
5
+ # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
6
+ # for a much simpler configuration file.
7
+ #
8
+ # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
9
+ # documentation.
10
+
11
+ # Use at least one worker per core if you're on a dedicated server,
12
+ # more will usually help for _short_ waits on databases/caches.
13
+ worker_processes 4
14
+
15
+ # Since Unicorn is never exposed to outside clients, it does not need to
16
+ # run on the standard HTTP port (80), there is no reason to start Unicorn
17
+ # as root unless it's from system init scripts.
18
+ # If running the master process as root and the workers as an unprivileged
19
+ # user, do this to switch euid/egid in the workers (also chowns logs):
20
+ # user "unprivileged_user", "unprivileged_group"
21
+
22
+ # Help ensure your application will always spawn in the symlinked
23
+ # "current" directory that Capistrano sets up.
24
+ working_directory "/path/to/app/current" # available in 0.94.0+
25
+
26
+ # listen on both a Unix domain socket and a TCP port,
27
+ # we use a shorter backlog for quicker failover when busy
28
+ listen "/path/to/.unicorn.sock", :backlog => 64
29
+ listen 8080, :tcp_nopush => true
30
+
31
+ # nuke workers after 30 seconds instead of 60 seconds (the default)
32
+ timeout 30
33
+
34
+ # feel free to point this anywhere accessible on the filesystem
35
+ pid "/path/to/app/shared/pids/unicorn.pid"
36
+
37
+ # By default, the Unicorn logger will write to stderr.
38
+ # Additionally, ome applications/frameworks log to stderr or stdout,
39
+ # so prevent them from going to /dev/null when daemonized here:
40
+ stderr_path "/path/to/app/shared/log/unicorn.stderr.log"
41
+ stdout_path "/path/to/app/shared/log/unicorn.stdout.log"
42
+
43
+ # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
44
+ # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
45
+ preload_app true
46
+ GC.respond_to?(:copy_on_write_friendly=) and
47
+ GC.copy_on_write_friendly = true
48
+
49
+ # Enable this flag to have unicorn test client connections by writing the
50
+ # beginning of the HTTP headers before calling the application. This
51
+ # prevents calling the application for connections that have disconnected
52
+ # while queued. This is only guaranteed to detect clients on the same
53
+ # host unicorn runs on, and unlikely to detect disconnects even on a
54
+ # fast LAN.
55
+ check_client_connection false
56
+
57
+ # local variable to guard against running a hook multiple times
58
+ run_once = true
59
+
60
+ before_fork do |server, worker|
61
+ # the following is highly recomended for Rails + "preload_app true"
62
+ # as there's no need for the master process to hold a connection
63
+ defined?(ActiveRecord::Base) and
64
+ ActiveRecord::Base.connection.disconnect!
65
+
66
+ # Occasionally, it may be necessary to run non-idempotent code in the
67
+ # master before forking. Keep in mind the above disconnect! example
68
+ # is idempotent and does not need a guard.
69
+ if run_once
70
+ # do_something_once_here ...
71
+ run_once = false # prevent from firing again
72
+ end
73
+
74
+ # The following is only recommended for memory/DB-constrained
75
+ # installations. It is not needed if your system can house
76
+ # twice as many worker_processes as you have configured.
77
+ #
78
+ # # This allows a new master process to incrementally
79
+ # # phase out the old master process with SIGTTOU to avoid a
80
+ # # thundering herd (especially in the "preload_app false" case)
81
+ # # when doing a transparent upgrade. The last worker spawned
82
+ # # will then kill off the old master process with a SIGQUIT.
83
+ # old_pid = "#{server.config[:pid]}.oldbin"
84
+ # if old_pid != server.pid
85
+ # begin
86
+ # sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
87
+ # Process.kill(sig, File.read(old_pid).to_i)
88
+ # rescue Errno::ENOENT, Errno::ESRCH
89
+ # end
90
+ # end
91
+ #
92
+ # Throttle the master from forking too quickly by sleeping. Due
93
+ # to the implementation of standard Unix signal handlers, this
94
+ # helps (but does not completely) prevent identical, repeated signals
95
+ # from being lost when the receiving process is busy.
96
+ # sleep 1
97
+ end
98
+
99
+ after_fork do |server, worker|
100
+ # per-process listener ports for debugging/admin/migrations
101
+ # addr = "127.0.0.1:#{9293 + worker.nr}"
102
+ # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
103
+
104
+ # the following is *required* for Rails + "preload_app true",
105
+ defined?(ActiveRecord::Base) and
106
+ ActiveRecord::Base.establish_connection
107
+
108
+ # if preload_app is true, then you may also want to check and
109
+ # restart any other shared sockets/descriptors such as Memcached,
110
+ # and Redis. TokyoCabinet file handles are safe to reuse
111
+ # between any number of forked children (assuming your kernel
112
+ # correctly implements pread()/pwrite() system calls)
113
+ end
@@ -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,124 @@
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
+
12
+ #define MIN(a,b) (a < b ? a : b)
13
+ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
14
+
15
+ #ifndef SIZEOF_OFF_T
16
+ # define SIZEOF_OFF_T 4
17
+ # warning SIZEOF_OFF_T not defined, guessing 4. Did you run extconf.rb?
18
+ #endif
19
+
20
+ #if SIZEOF_OFF_T == 4
21
+ # define UH_OFF_T_MAX 0x7fffffff
22
+ #elif SIZEOF_OFF_T == 8
23
+ # if SIZEOF_LONG == 4
24
+ # define UH_OFF_T_MAX 0x7fffffffffffffffLL
25
+ # else
26
+ # define UH_OFF_T_MAX 0x7fffffffffffffff
27
+ # endif
28
+ #else
29
+ # error off_t size unknown for this platform!
30
+ #endif /* SIZEOF_OFF_T check */
31
+
32
+ /*
33
+ * ragel enforces fpc as a const, and merely casting can make picky
34
+ * compilers unhappy, so we have this little helper do our dirty work
35
+ */
36
+ static inline void *deconst(const void *in)
37
+ {
38
+ union { const void *in; void *out; } tmp;
39
+
40
+ tmp.in = in;
41
+
42
+ return tmp.out;
43
+ }
44
+
45
+ /*
46
+ * capitalizes all lower-case ASCII characters and converts dashes
47
+ * to underscores for HTTP headers. Locale-agnostic.
48
+ */
49
+ static void snake_upcase_char(char *c)
50
+ {
51
+ if (*c >= 'a' && *c <= 'z')
52
+ *c &= ~0x20;
53
+ else if (*c == '-')
54
+ *c = '_';
55
+ }
56
+
57
+ /* Downcases a single ASCII character. Locale-agnostic. */
58
+ static void downcase_char(char *c)
59
+ {
60
+ if (*c >= 'A' && *c <= 'Z')
61
+ *c |= 0x20;
62
+ }
63
+
64
+ static int hexchar2int(int xdigit)
65
+ {
66
+ if (xdigit >= 'A' && xdigit <= 'F')
67
+ return xdigit - 'A' + 10;
68
+ if (xdigit >= 'a' && xdigit <= 'f')
69
+ return xdigit - 'a' + 10;
70
+
71
+ /* Ragel already does runtime range checking for us in Unicorn: */
72
+ assert(xdigit >= '0' && xdigit <= '9' && "invalid digit character");
73
+
74
+ return xdigit - '0';
75
+ }
76
+
77
+ /*
78
+ * multiplies +i+ by +base+ and increments the result by the parsed
79
+ * integer value of +xdigit+. +xdigit+ is a character byte
80
+ * representing a number the range of 0..(base-1)
81
+ * returns the new value of +i+ on success
82
+ * returns -1 on errors (including overflow)
83
+ */
84
+ static off_t step_incr(off_t i, int xdigit, const int base)
85
+ {
86
+ static const off_t max = UH_OFF_T_MAX;
87
+ const off_t next_max = (max - (max % base)) / base;
88
+ off_t offset = hexchar2int(xdigit);
89
+
90
+ if (offset > (base - 1))
91
+ return -1;
92
+ if (i > next_max)
93
+ return -1;
94
+ i *= base;
95
+
96
+ if ((offset > (base - 1)) || ((max - i) < offset))
97
+ return -1;
98
+
99
+ return i + offset;
100
+ }
101
+
102
+ /*
103
+ * parses a non-negative length according to base-10 and
104
+ * returns it as an off_t value. Returns -1 on errors
105
+ * (including overflow).
106
+ */
107
+ static off_t parse_length(const char *value, size_t length)
108
+ {
109
+ off_t rv;
110
+
111
+ for (rv = 0; length-- && rv >= 0; ++value) {
112
+ if (*value >= '0' && *value <= '9')
113
+ rv = step_incr(rv, *value, 10);
114
+ else
115
+ return -1;
116
+ }
117
+
118
+ return rv;
119
+ }
120
+
121
+ #define CONST_MEM_EQ(const_p, buf, len) \
122
+ ((sizeof(const_p) - 1) == len && !memcmp(const_p, buf, sizeof(const_p) - 1))
123
+
124
+ #endif /* UH_util_h */