PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/README.markdown

http://github.com/nicolasff/webdis
Markdown | 356 lines | 291 code | 65 blank | 0 comment | 0 complexity | 5350545ce8e658217aba81d286564ca3 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, JSON
  1. [![CircleCI](https://circleci.com/gh/nicolasff/webdis.svg?style=shield)](https://circleci.com/gh/nicolasff/webdis)
  2. # About
  3. A very simple web server providing an HTTP interface to Redis. It uses [hiredis](https://github.com/antirez/hiredis), [jansson](https://github.com/akheron/jansson), [libevent](http://monkey.org/~provos/libevent/), and [http-parser](https://github.com/ry/http-parser/).
  4. Webdis depends on libevent-dev. You can install it on Ubuntu by typing `sudo apt-get install libevent-dev` or on OS X by typing `brew install libevent`.
  5. <pre>
  6. make clean all
  7. ./webdis &
  8. curl http://127.0.0.1:7379/SET/hello/world
  9. {"SET":[true,"OK"]}
  10. curl http://127.0.0.1:7379/GET/hello
  11. {"GET":"world"}
  12. curl -d "GET/hello" http://127.0.0.1:7379/
  13. {"GET":"world"}
  14. </pre>
  15. # Try in Docker
  16. <pre>
  17. $ docker run --rm -d -p 7379:7379 nicolas/webdis
  18. 0d2ce311a4834d403cc3e7cfd571b168ba40cede6a0e155a21507bb0bf7bee81
  19. $ curl http://127.0.0.1:7379/PING
  20. {"PING":[true,"PONG"]}
  21. # To stop it:
  22. $ docker stop $(docker ps | grep webdis | cut -c 1-12)
  23. 0d2ce311a483
  24. </pre>
  25. # Build and run Docker image locally
  26. Clone the repository and open a terminal in the webdis directory, then run:
  27. <pre>
  28. $ docker build -t webdis .
  29. [...]
  30. $ docker run --rm -d -p 7379:7379 webdis
  31. f0a2763fd456ac1f7ebff80eeafd6a5cd0fc7f06c69d0f7717fb2bdcec65926e
  32. $ curl http://127.0.0.1:7379/PING
  33. {"PING":[true,"PONG"]}
  34. # To stop it:
  35. $ docker stop $(docker ps | grep webdis | cut -c 1-12)
  36. f0a2763fd456
  37. </pre>
  38. # Features
  39. * `GET` and `POST` are supported, as well as `PUT` for file uploads.
  40. * JSON output by default, optional JSONP parameter (`?jsonp=myFunction` or `?callback=myFunction`).
  41. * Raw Redis 2.0 protocol output with `.raw` suffix
  42. * MessagePack output with `.msg` suffix
  43. * HTTP 1.1 pipelining (70,000 http requests per second on a desktop Linux machine.)
  44. * Multi-threaded server, configurable number of worker threads.
  45. * WebSocket support (Currently using the hixie-76 specification).
  46. * Connects to Redis using a TCP or UNIX socket.
  47. * Restricted commands by IP range (CIDR subnet + mask) or HTTP Basic Auth, returning 403 errors.
  48. * Possible Redis authentication in the config file.
  49. * Pub/Sub using `Transfer-Encoding: chunked`, works with JSONP as well. Webdis can be used as a Comet server.
  50. * Drop privileges on startup.
  51. * Custom Content-Type using a pre-defined file extension, or with `?type=some/thing`.
  52. * URL-encoded parameters for binary data or slashes and question marks. For instance, `%2f` is decoded as `/` but not used as a command separator.
  53. * Logs, with a configurable verbosity.
  54. * Cross-origin requests, usable with XMLHttpRequest2 (Cross-Origin Resource Sharing - CORS).
  55. * File upload with PUT.
  56. * With the JSON output, the return value of INFO is parsed and transformed into an object.
  57. * Optional daemonize: set `"daemonize": true` and `"pidfile": "/var/run/webdis.pid"` in webdis.json.
  58. * Default root object: Add `"default_root": "/GET/index.html"` in webdis.json to substitute the request to `/` with a Redis request.
  59. * HTTP request limit with `http_max_request_size` (in bytes, set to 128MB by default).
  60. * Database selection in the URL, using e.g. `/7/GET/key` to run the command on DB 7.
  61. # Ideas, TODO...
  62. * Add better support for PUT, DELETE, HEAD, OPTIONS? How? For which commands?
  63. * This could be done using a strict mode with a table of commands and the verbs that can/must be used with each command. Strict mode would be optional, configurable. How would webdis know of new commands remains to be determined.
  64. * MULTI/EXEC/DISCARD/WATCH are disabled at the moment; find a way to use them.
  65. * Support POST of raw Redis protocol data, and execute the whole thing. This could be useful for MULTI/EXEC transactions.
  66. * Enrich config file:
  67. * Provide timeout (maybe for some commands only?). What should the response be? 504 Gateway Timeout? 503 Service Unavailable?
  68. * Multi-server support, using consistent hashing.
  69. * SSL?
  70. * Not sure if this is such a good idea.
  71. * SPDY?
  72. * SPDY is mostly useful for parallel fetches. Not sure if it would make sense for Webdis.
  73. * Send your ideas using the github tracker, on twitter [@yowgi](http://twitter.com/yowgi) or by mail to n.favrefelix@gmail.com.
  74. # HTTP error codes
  75. * Unknown HTTP verb: 405 Method Not Allowed.
  76. * Redis is unreachable: 503 Service Unavailable.
  77. * Matching ETag sent using `If-None-Match`: 304 Not Modified.
  78. * Could also be used:
  79. * Timeout on the redis side: 503 Service Unavailable.
  80. * Missing key: 404 Not Found.
  81. * Unauthorized command (disabled in config file): 403 Forbidden.
  82. # Command format
  83. The URI `/COMMAND/arg0/arg1/.../argN.ext` executes the command on Redis and returns the response to the client. GET, POST, and PUT are supported:
  84. * `GET /COMMAND/arg0/.../argN.ext`
  85. * `POST /` with `COMMAND/arg0/.../argN` in the HTTP body.
  86. * `PUT /COMMAND/arg0.../argN-1` with `argN` in the HTTP body (see section on [file uploads](#file-upload).)
  87. `.ext` is an optional extension; it is not read as part of the last argument but only represents the output format. Several formats are available (see below).
  88. Special characters: `/` and `.` have special meanings, `/` separates arguments and `.` changes the Content-Type. They can be replaced by `%2f` and `%2e`, respectively.
  89. # ACL
  90. Access control is configured in `webdis.json`. Each configuration tries to match a client profile according to two criterias:
  91. * [CIDR](http://en.wikipedia.org/wiki/CIDR) subnet + mask
  92. * [HTTP Basic Auth](http://en.wikipedia.org/wiki/Basic_access_authentication) in the format of "user:password".
  93. Each ACL contains two lists of commands, `enabled` and `disabled`. All commands being enabled by default, it is up to the administrator to disable or re-enable them on a per-profile basis.
  94. Examples:
  95. <pre>
  96. {
  97. "disabled": ["DEBUG", "FLUSHDB", "FLUSHALL"],
  98. },
  99. {
  100. "http_basic_auth": "user:password",
  101. "disabled": ["DEBUG", "FLUSHDB", "FLUSHALL"],
  102. "enabled": ["SET"]
  103. },
  104. {
  105. "ip": "192.168.10.0/24",
  106. "enabled": ["SET"]
  107. },
  108. {
  109. "http_basic_auth": "user:password",
  110. "ip": "192.168.10.0/24",
  111. "enabled": ["SET", "DEL"]
  112. }
  113. </pre>
  114. ACLs are interpreted in order, later authorizations superseding earlier ones if a client matches several. The special value "*" matches all commands.
  115. # JSON output
  116. JSON is the default output format. Each command returns a JSON object with the command as a key and the result as a value.
  117. **Examples:**
  118. <pre>
  119. // string
  120. $ curl http://127.0.0.1:7379/GET/y
  121. {"GET":"41"}
  122. // number
  123. $ curl http://127.0.0.1:7379/INCR/y
  124. {"INCR":42}
  125. // list
  126. $ curl http://127.0.0.1:7379/LRANGE/x/0/1
  127. {"LRANGE":["abc","def"]}
  128. // status
  129. $ curl http://127.0.0.1:7379/TYPE/y
  130. {"TYPE":[true,"string"]}
  131. // error, which is basically a status
  132. $ curl http://127.0.0.1:7379/MAKE-ME-COFFEE
  133. {"MAKE-ME-COFFEE":[false,"ERR unknown command 'MAKE-ME-COFFEE'"]}
  134. // JSONP callback:
  135. $ curl "http://127.0.0.1:7379/TYPE/y?jsonp=myCustomFunction"
  136. myCustomFunction({"TYPE":[true,"string"]})
  137. </pre>
  138. # RAW output
  139. This is the raw output of Redis; enable it with the `.raw` suffix.
  140. <pre>
  141. // string
  142. $ curl http://127.0.0.1:7379/GET/z.raw
  143. $5
  144. hello
  145. // number
  146. curl http://127.0.0.1:7379/INCR/a.raw
  147. :2
  148. // list
  149. $ curl http://127.0.0.1:7379/LRANGE/x/0/-1.raw
  150. *2
  151. $3
  152. abc
  153. $3
  154. def
  155. // status
  156. $ curl http://127.0.0.1:7379/TYPE/y.raw
  157. +zset
  158. // error, which is basically a status
  159. $ curl http://127.0.0.1:7379/MAKE-ME-COFFEE.raw
  160. -ERR unknown command 'MAKE-ME-COFFEE'
  161. </pre>
  162. # Custom content-type
  163. Several content-types are available:
  164. * `.json` for `application/json` (this is the default Content-Type).
  165. * `.msg` for `application/x-msgpack`. See [http://msgpack.org/](http://msgpack.org/) for the specs.
  166. * `.txt` for `text/plain`
  167. * `.html` for `text/html`
  168. * `xhtml` for `application/xhtml+xml`
  169. * `xml` for `text/xml`
  170. * `.png` for `image/png`
  171. * `jpg` or `jpeg` for `image/jpeg`
  172. * Any other with the `?type=anything/youwant` query string.
  173. * Add a custom separator for list responses with `?sep=,` query string.
  174. <pre>
  175. curl -v "http://127.0.0.1:7379/GET/hello.html"
  176. [...]
  177. &lt; HTTP/1.1 200 OK
  178. &lt; Content-Type: text/html
  179. &lt; Date: Mon, 03 Jan 2011 20:43:36 GMT
  180. &lt; Content-Length: 137
  181. &lt;
  182. &lt;!DOCTYPE html&gt;
  183. &lt;html&gt;
  184. [...]
  185. &lt;/html&gt;
  186. curl -v "http://127.0.0.1:7379/GET/hello.txt"
  187. [...]
  188. &lt; HTTP/1.1 200 OK
  189. &lt; Content-Type: text/plain
  190. &lt; Date: Mon, 03 Jan 2011 20:43:36 GMT
  191. &lt; Content-Length: 137
  192. [...]
  193. curl -v "http://127.0.0.1:7379/GET/big-file?type=application/pdf"
  194. [...]
  195. &lt; HTTP/1.1 200 OK
  196. &lt; Content-Type: application/pdf
  197. &lt; Date: Mon, 03 Jan 2011 20:45:12 GMT
  198. [...]
  199. </pre>
  200. # File upload
  201. Webdis supports file upload using HTTP PUT. The command URI is slightly different, as the last argument is taken from the HTTP body.
  202. For example: instead of `/SET/key/value`, the URI becomes `/SET/key` and the value is the entirety of the body. This works for other commands such as LPUSH, etc.
  203. **Uploading a binary file to webdis**:
  204. <pre>
  205. $ file redis-logo.png
  206. redis-logo.png: PNG image, 513 x 197, 8-bit/color RGBA, non-interlaced
  207. $ wc -c redis-logo.png
  208. 16744 redis-logo.png
  209. $ curl -v --upload-file redis-logo.png http://127.0.0.1:7379/SET/logo
  210. [...]
  211. &gt; PUT /SET/logo HTTP/1.1
  212. &gt; User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
  213. &gt; Host: 127.0.0.1:7379
  214. &gt; Accept: */*
  215. &gt; Content-Length: 16744
  216. &gt; Expect: 100-continue
  217. &gt;
  218. &lt; HTTP/1.1 100 Continue
  219. &lt; HTTP/1.1 200 OK
  220. &lt; Content-Type: application/json
  221. &lt; ETag: "0db1124cf79ffeb80aff6d199d5822f8"
  222. &lt; Date: Sun, 09 Jan 2011 16:48:19 GMT
  223. &lt; Content-Length: 19
  224. &lt;
  225. {"SET":[true,"OK"]}
  226. $ curl -vs http://127.0.0.1:7379/GET/logo.png -o out.png
  227. &gt; GET /GET/logo.png HTTP/1.1
  228. &gt; User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
  229. &gt; Host: 127.0.0.1:7379
  230. &gt; Accept: */*
  231. &gt;
  232. &lt; HTTP/1.1 200 OK
  233. &lt; Content-Type: image/png
  234. &lt; ETag: "1991df597267d70bf9066a7d11969da0"
  235. &lt; Date: Sun, 09 Jan 2011 16:50:51 GMT
  236. &lt; Content-Length: 16744
  237. $ md5sum redis-logo.png out.png
  238. 1991df597267d70bf9066a7d11969da0 redis-logo.png
  239. 1991df597267d70bf9066a7d11969da0 out.png
  240. </pre>
  241. The file was uploaded and re-downloaded properly: it has the same hash and the content-type was set properly thanks to the `.png` extension.
  242. # WebSockets
  243. Webdis supports WebSocket clients implementing [dixie-76](http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76).
  244. Web Sockets are supported with the following formats, selected by the connection URL:
  245. * JSON (on `/` or `/.json`)
  246. * Raw Redis wire protocol (on `/.raw`)
  247. **Example**:
  248. <pre>
  249. function testJSON() {
  250. var jsonSocket = new WebSocket("ws://127.0.0.1:7379/.json");
  251. jsonSocket.onopen = function() {
  252. console.log("JSON socket connected!");
  253. jsonSocket.send(JSON.stringify(["SET", "hello", "world"]));
  254. jsonSocket.send(JSON.stringify(["GET", "hello"]));
  255. };
  256. jsonSocket.onmessage = function(messageEvent) {
  257. console.log("JSON received:", messageEvent.data);
  258. };
  259. }
  260. testJSON();
  261. </pre>
  262. This produces the following output:
  263. <pre>
  264. JSON socket connected!
  265. JSON received: {"SET":[true,"OK"]}
  266. JSON received: {"GET":"world"}
  267. </pre>
  268. # Pub/Sub with chunked transfer encoding
  269. Webdis exposes Redis PUB/SUB channels to HTTP clients, forwarding messages in the channel as they are published by Redis. This is done using chunked transfer encoding.
  270. **Example using XMLHttpRequest**:
  271. <pre>
  272. var previous_response_length = 0
  273. xhr = new XMLHttpRequest()
  274. xhr.open("GET", "http://127.0.0.1:7379/SUBSCRIBE/hello", true);
  275. xhr.onreadystatechange = checkData;
  276. xhr.send(null);
  277. function checkData() {
  278. if(xhr.readyState == 3) {
  279. response = xhr.responseText;
  280. chunk = response.slice(previous_response_length);
  281. previous_response_length = response.length;
  282. console.log(chunk);
  283. }
  284. };
  285. </pre>
  286. Publish messages to redis to see output similar to the following:
  287. <pre>
  288. {"SUBSCRIBE":["subscribe","hello",1]}
  289. {"SUBSCRIBE":["message","hello","some message"]}
  290. {"SUBSCRIBE":["message","hello","some other message"]}
  291. </pre>