17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

286<br />

The get entry po<strong>in</strong>t is, perhaps, the simplest of the three. This is used for retriev<strong>in</strong>g a a value<br />

from the cache. It expects the request to come <strong>in</strong> as an GET request with the follow<strong>in</strong>g<br />

format.<br />

GET /key<br />

If we receive this request then we return the value <strong>in</strong> the cache associated with that key.<br />

The delete entry po<strong>in</strong>t follows closely the format of the get request, with the exception<br />

that it expectes it to come <strong>in</strong> as an HTTP DELETE request. We expect a url of the follow<strong>in</strong>g<br />

form.<br />

DELETE /key<br />

If we receive this request we are expected to delete the value specified by key from the<br />

cache.<br />

F<strong>in</strong>ally we have the most complex of the entry po<strong>in</strong>ts, the put entry po<strong>in</strong>t. We expect this<br />

request to come <strong>in</strong> as an HTTP PUT request. The ma<strong>in</strong> difference between this entry po<strong>in</strong>t<br />

<strong>and</strong> the get/put entry po<strong>in</strong>t is that the message has a body. That body will conta<strong>in</strong> the value<br />

we are expected to <strong>in</strong>sert <strong>in</strong>to the cache. We expect a url of the follow<strong>in</strong>g form.<br />

PUT /key<br />

If we receive this request we are expected to <strong>in</strong>sert a new value <strong>in</strong>to the simple_cache.<br />

This value will be identified by the ‘key’ <strong>in</strong> the url with a value specified by the body of the<br />

message.<br />

Implement<strong>in</strong>g Our Protocol <strong>in</strong> the HTTP Server<br />

We implement our protocol <strong>in</strong> the h<strong>and</strong>le_message function of our HTTP server. The code<br />

is listed <strong>in</strong> code list<strong>in</strong>g 11.10.<br />

Code list<strong>in</strong>g 11.10 – h<strong>and</strong>le_message/2<br />

h<strong>and</strong>le_message({http_request, 'GET', {abs_path, [$/|Key]}, _}, _Body) -><br />

case simple_cache:lookup(Key) of<br />

{ok, Value} -> create_http_message(200, "text/html", Value);<br />

{error, not_found} -> create_http_message(404)<br />

end;<br />

h<strong>and</strong>le_message({http_request,'PUT',{abs_path, [$/|Key]},_}, Body) -><br />

simple_cache:<strong>in</strong>sert(Key, Body),<br />

create_http_message(200);<br />

h<strong>and</strong>le_message({http_request, 'DELETE', {abs_path, [$/|Key]}, _}, _Body) -><br />

simple_cache:delete(Key),<br />

create_http_message(200).<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!