{"id":484,"date":"2009-10-19T18:52:59","date_gmt":"2009-10-19T22:52:59","guid":{"rendered":"http:\/\/steve.vinoski.net\/blog\/?p=484"},"modified":"2009-10-19T18:52:59","modified_gmt":"2009-10-19T22:52:59","slug":"yaws-1-85-released","status":"publish","type":"post","link":"https:\/\/steve.vinoski.net\/blog\/2009\/10\/19\/yaws-1-85-released\/","title":{"rendered":"Yaws 1.85 Released"},"content":{"rendered":"<p>Today Klacke announced <a href=\"http:\/\/yaws.hyber.org\/\">Yaws 1.85<\/a>, mainly a bugfix release. You can find the list of changes and fixes at that link, but one addition in this release I wanted to point out was our new <code> streamcontent_from_pid<\/code> feature, which allows your server application code to temporarily take over the client connection socket from Yaws, thus allowing you to feed data directly to the socket without first passing it back through Yaws. Could be just the ticket for long-polling (Comet) applications, for example.<\/p>\n<p>Prior to this release, the closest feature Yaws provided for this sort of operation was the <code>streamcontent<\/code> capability, which is still very useful in that it allows you to have an Erlang process deliver data back into Yaws, which in turn sends it in HTTP chunked transfer mode back to the client. For large file resources like video files or install tarballs, or for data sources where content arrives at your server in batches from a separate back-end source, or generally for resources whose sizes are not known up front, <code>streamcontent<\/code> is perfect because it lets you transfer the data back into Yaws in chunks, at your leisure and without having to copy all the data at once. Still, though, the data has to be sent back through Yaws, which converts it to HTTP chunks and writes it to the socket, plus in this case your only choice is chunked transfer.<\/p>\n<p>With <code>streamcontent_from_pid<\/code>, you reply to the <code>out\/1<\/code> upcall from Yaws with the HTTP reply headers and with the following special return tuple:<\/p>\n<pre><code>{streamcontent_from_pid, MimeType, StreamPid}<\/code><\/pre>\n<p>This tells Yaws that you wish to have process <code>StreamPid<\/code> take over the client socket in order to send data of media type <code>MimeType<\/code> directly back to the client. Yaws uses <code>MimeType<\/code> to set the HTTP <code>Content-Type<\/code> header, and then after sending that and all the other HTTP headers back to the client, it turns control of the client socket over to <code>StreamPid<\/code>. The code running within <code>StreamPid<\/code> must handle the following messages from Yaws:<\/p>\n<ul>\n<li><code>{ok, YawsPid}<\/code> tells <code>StreamPid<\/code> that it can proceed with using the socket. The socket is present in the original <code>Arg<\/code> variable passed to your <code>out\/1<\/code> function and can be retrieved via <code>Arg#arg.clisock<\/code>.<\/li>\n<li><code>{discard, YawsPid}<\/code> tells <code>StreamPid<\/code> that it shouldn&#8217;t send any data on the socket, for example because the client request was an HTTP <code>HEAD<\/code> request and so there is no response body.<\/li>\n<\/ul>\n<p>To send data, your code can choose to send chunked data or non-chunked data. To send the latter, first make sure you set the HTTP <code>Content-Length<\/code> header in your initial reply to Yaws, and then once Yaws calls back to your <code>StreamPid<\/code> process, just call:<\/p>\n<pre><code>yaws_api:stream_process_deliver(Socket, IoList)<\/code><\/pre>\n<p>or for chunked data:<\/p>\n<pre><code>yaws_api:stream_process_deliver_chunk(Socket, IoList)<\/code><\/pre>\n<p>where for both cases <code>Socket<\/code> is the client socket from the <code>Arg<\/code> and <code>IoList<\/code> is an iolist containing the data to be sent. The first case just calls <code>gen_tcp:send<\/code> and is there primarily so we can maybe someday add SSL socket support for this feature. For the second case Yaws will format the data for you for chunked transfer. Unless a <code>Content-Length<\/code> header is set, Yaws will assume you want chunked transfer and will set the <code>Transfer-Encoding<\/code> header appropriately. If you&#8217;re sending chunked data, make sure you send your final chunk using the following function:<\/p>\n<pre><code>yaws_api:stream_process_deliver_final_chunk(Socket, IoList)<\/code><\/pre>\n<p>so that Yaws knows to send the termination chunk to inform the client of the end of the transfer.<\/p>\n<p>You can continue to call these functions from your <code>StreamPid<\/code> as frequently as you need to in order to deliver your data to the client. Meanwhile, the Yaws process that handed you the socket will just sit back and wait for you (non-blocking, of course). When you&#8217;re completely finished sending, just call:<\/p>\n<pre><code>yaws_api:stream_process_end(Socket, YawsPid)<\/code><\/pre>\n<p>to end the transmission and give control of the socket back to Yaws. At that point, your <code>StreamPid<\/code> can exit if it wishes.<\/p>\n<p>If you try out this feature, be sure to send feedback either to me or to the <a href=\"https:\/\/lists.sourceforge.net\/lists\/listinfo\/erlyaws-list\">Yaws mailing list<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today Klacke announced Yaws 1.85, mainly a bugfix release. You can find the list of changes and fixes at that link, but one addition in this release I wanted to point out was our new streamcontent_from_pid feature, which allows your server application code to temporarily take over the client connection socket from Yaws, thus allowing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,33,84],"tags":[162,160,175],"class_list":["post-484","post","type-post","status-publish","format-standard","hentry","category-http","category-web","category-yaws","tag-http","tag-web","tag-yaws"],"_links":{"self":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/comments?post=484"}],"version-history":[{"count":22,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/media?parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/categories?post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/tags?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}