about summary refs log blame commit diff stats
path: root/html/http-server.mu.html
blob: f3192ed56d5d04a9eaab40e4de98198f368d9d98 (plain) (tree)
1
2
3
4
5



                                                                                          
                                  









                                                                                                 
                              
                              
                            

                             
                             










                               




                                                                                          
                              


                                                                                                        

                                           
                                                                                                           
                                                                                                                       
                                                     
                                                               


                                                                                                                                  
                                  


                                                                                 

                                       









                                                                                                                                                           
 
 
                                                                              

                                                

                                                                               
                                  
                                                                         
                                                 
                                                         
                                                                                                                                                 




                                                                               




                                                                                                                                                   
                                                                                                                                   
                                                                                                                                                          
                                                                                 
                                    
                                                                                  










                                                                                                                 



                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - http-server.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Delimiter { color: #800080; }
.muControl { color: #c0a020; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># example program: a single-request HTTP server</span>
<span class="Comment">#   listen for connections from clients on a server socket</span>
<span class="Comment">#   when a connection occurs, transfer it to a session socket</span>
<span class="Comment">#   read from it using channels</span>
<span class="Comment">#   write to it directly</span>
<span class="Comment">#</span>
<span class="Comment"># After running it, navigate to localhost:8080. Your browser should display</span>
<span class="Comment"># &quot;SUCCESS!&quot; and the server will terminate after one connection.</span>

<span class="muRecipe">def</span> main [
  <span class="Constant">local-scope</span>
  socket:num<span class="Special"> &lt;- </span>$open-server-socket <span class="Constant">8080/port</span>
  $print <span class="Constant">[Mu socket creation returned ]</span>, socket, <span class="Constant">10/newline</span>
  <span class="muControl">return-unless</span> socket
  session:num<span class="Special"> &lt;- </span>$accept socket
  contents:&amp;:source:char, sink:&amp;:sink:char<span class="Special"> &lt;- </span>new-channel <span class="Constant">30</span>
  sink<span class="Special"> &lt;- </span>start-running transmit-from-socket session, sink
  buf:&amp;:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">30</span>
  <span class="Delimiter">{</span>
    c:char, done?:bool, contents<span class="Special"> &lt;- </span>read contents
    <span class="muControl">break-if</span> done?
    buf<span class="Special"> &lt;- </span>append buf, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  socket-text:text<span class="Special"> &lt;- </span>buffer-to-array buf
  $print <span class="Constant">[Done reading from socket.]</span>, <span class="Constant">10/newline</span>
  write-to-socket session, <span class="Constant">[HTTP/1.0 200 OK</span>
<span class="Constant">Content-type: text/plain</span>

<span class="Constant">SUCCESS!</span>
<span class="Constant">]</span>
  $print <span class="Constant">10/newline</span>, <span class="Constant">[Wrote to and closing socket...]</span>, <span class="Constant">10/newline</span>
  $close-socket session
  $close-socket socket
]

<span class="muRecipe">def</span> write-to-socket session-socket:num, s:text [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  len:num<span class="Special"> &lt;- </span>length *s
  i:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    done?:bool<span class="Special"> &lt;- </span>greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    c:char<span class="Special"> &lt;- </span>index *s, i
    $print <span class="Constant">[writing to socket: ]</span>, i, <span class="Constant">[ ]</span>, c, <span class="Constant">10/newline</span>
    $write-to-socket session-socket, c
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
]

<span class="muRecipe">def</span> transmit-from-socket session:num, sink:&amp;:sink:char<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    req:text, bytes-read:num<span class="Special"> &lt;- </span>$read-from-socket session, <span class="Constant">4096/bytes</span>
    $print <span class="Constant">[read ]</span>, bytes-read, <span class="Constant">[ bytes from socket]</span>, <span class="Constant">10/newline</span>
    i:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
    <span class="Delimiter">{</span>
      done?:bool<span class="Special"> &lt;- </span>greater-or-equal i, bytes-read
      <span class="muControl">break-if</span> done?
      c:char<span class="Special"> &lt;- </span>index *req, i
      end-of-request?:bool<span class="Special"> &lt;- </span>equal c, <span class="Constant">10/newline</span>
      <span class="muControl">break-if</span> end-of-request? <span class="Comment"># To be safe, for now.</span>
      sink<span class="Special"> &lt;- </span>write sink, c
      i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  sink<span class="Special"> &lt;- </span>close sink
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->