src/mongrel2.nim
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 27 Oct 2021 13:57:16 -0700
changeset 6 d49437ff8f2f
parent 4 ffb8b9920057
permissions -rw-r--r--
Updates for more recent (v1.6.0) nim.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
     1
# vim: set et sta sw=4 ts=4 :
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
#
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
     3
# Copyright (c) 2015-2021, Mahlon E. Smith <mahlon@martini.nu>
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
# All rights reserved.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
# Redistribution and use in source and binary forms, with or without
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# modification, are permitted provided that the following conditions are met:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
#     * Redistributions of source code must retain the above copyright
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
#       notice, this list of conditions and the following disclaimer.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
#     * Redistributions in binary form must reproduce the above copyright
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
#       notice, this list of conditions and the following disclaimer in the
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
#       documentation and/or other materials provided with the distribution.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
#     * Neither the name of Mahlon E. Smith nor the names of his
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
#       contributors may be used to endorse or promote products derived
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
#       from this software without specific prior written permission.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
## This module is a simple interface to the Mongrel2 webserver
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
## environment (http://mongrel2.org/).  After a Mongrel2 server has been
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
## properly configured, you can use this library to easily service client
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
## requests.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
## Bare minimal, do-nothing example:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
## .. code-block:: nim
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
##    newM2Handler( "app-id", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" ).run
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
## Yep, that's it.  Assuming your Mongrel2 server is configured with a
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
## matching application identifier and request/response communication
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
## ports, that's enough to see the default output from the handler when
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
## loaded in browser.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
## It's likely that you'll want to do something of more substance.  This
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
## is performed via an `action` hook, which is just a proc() reference.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
## It is passed the parsed `M2Request` client request, and it needs to
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
## return a matching `M2Response` object.  What happens in between is
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
## entirely up to you.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
## Here's a "hello world":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
## .. code-block:: nim
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
##    let handler = newM2Handler( "app-id", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
##    
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
##    proc hello_world( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
##        result = request.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
##        result[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
##        result.body = "Hello there, world!"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
##        result.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
##    
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
##    handler.action = hello_world
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
##    handler.run
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
##    
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
## And finally, a slightly more interesting example:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
## .. code-block:: nim
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
##   import
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
##       mongrel2,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
##       json,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
##       re
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
##   let handler = newM2Handler( "app-id", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
##   var data    = %*[] ## the JSON data to "remember"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
##   proc demo( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
##       ## This is a demonstration handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
##       ##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
##       ## It accepts and stores a JSON data structure
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
##       ## on POST, and returns it on GET.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
##       # Create a response object for the current request.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
##       var response = request.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
##       case request.meth
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
##       # For GET requests, display the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
##       #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
##       of "GET":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
##           if request[ "Accept" ].match( re(".*text/(html|plain).*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
##               response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
##               response.body   = "Hi there.  POST some JSON to me and I'll remember it.\n\n" & $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
##               response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
##           elif request[ "Accept" ].match( re("application/json") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
##               response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
##               response.body   = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
##               response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
##           else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
##               response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
##       # POST requests overwrite the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
##       #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
##       of "POST":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
##           if request[ "Content-Type" ].match( re(".*application/json.*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
##               try:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
##                   data = request.body.parse_json
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
##                   response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
##                   response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
##                   response.body = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
##               except:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
##                   response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
##                   response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
##                   response.body = request.body
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
##           else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
##               response.body   = "I only accept valid JSON strings."
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
##               response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
##       else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
##           response.status = HTTP_NOT_ACCEPTABLE
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
##       return response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
##   # Attach the proc reference to the handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
##   handler.action = demo
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
##   
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
##   # Start 'er up!
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
##   handler.run
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
import
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   140
    std/json,
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   141
    std/strutils,
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   142
    std/tables,
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   143
    std/times,
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
    tnetstring,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
    zmq
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
type
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
    M2Handler* = ref object of RootObj
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
        handler_id:         string
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   150
        request_sock:       ZConnection
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   151
        response_sock:      ZConnection
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
        action*:            proc ( request: M2Request ): M2Response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
        disconnect_action*: proc ( request: M2Request )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
    M2Request* = ref object of RootObj
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
        sender_id*:   string  ## Mongrel2 app-id
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
        conn_id*:     string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
        path*:        string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
        meth*:        string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
        version*:     string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
        uri*:         string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
        pattern*:     string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
        scheme*:      string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
        remote_addr*: string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
        query*:       string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
        headers*:     seq[ tuple[name: string, value: string] ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
        body*:        string 
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
    M2Response* = ref object of RootObj
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
        sender_id*: string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
        conn_id*:   string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
        status*:    int
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
        headers*:   seq[ tuple[name: string, value: string] ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
        body*:      string 
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
        extended:   string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
        ex_data:    seq[ string ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
const 
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
    MAX_CLIENT_BROADCAST = 128 ## The maximum number of client to broadcast a response to
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
    CRLF = "\r\n"              ## Network line ending
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
    DEFAULT_CONTENT = """
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
<!DOCTYPE html>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
<html lang="en">
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
    <head>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
        <title>It works!</title>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
        <link href='http://fonts.googleapis.com/css?family=Play' rel='stylesheet' type='text/css'>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
        <style>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
        body {
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
            margin: 50px;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
            height: 100%;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
            background-color: #ddd;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
            font-family: Play, Arial, serif;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
            font-weight: 400;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
            background: linear-gradient( to bottom, #7db9e8 25%,#fff 100% );
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
        }
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
        a, a:hover, a:visited {
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
            text-decoration: none;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
            color: rgb( 66,131,251 );
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
        }
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
        .content {
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
            font-size: 1.2em;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
            border-radius: 10px;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
            margin: 0 auto 0 auto;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
            width: 50%;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
            padding: 5px 20px 20px 20px;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   206
            box-shadow: 1px 2px 6px #000;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   207
            background-color: #fff;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   208
            border: 1px;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
        }
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
        .handler {
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   211
            font-size: 0.9em;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   212
            padding: 2px 0 0 10px;
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   213
            color: rgb( 192,220,255 );
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   214
            background-color: rgb( 6,20,85 );
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
            border: 2px solid rgb( 66,131,251 );
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
        }
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
        </style>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
    </head>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
    <body>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
        <div class="content">
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
            <h1>Almost there...</h1>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
            <p>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
                This is the default handler output.  While this is
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
                useful to demonstrate that your <a href="http://mongrel2.org">Mongrel2</a>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
                server is indeed speaking to your <a href="http://nim-lang.org">nim</a>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
                handler, you're probably going to want to do something else for production use.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
            </p>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
            <p>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   229
                Here's an example handler:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
            </p>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
            <div class="handler">
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
            <pre>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
import
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
    mongrel2,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
    json,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
    re
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
let handler = newM2Handler( "app-id", "tcp://127.0.0.1:9009", "tcp://127.0.0.1:9008" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
var data    = %*[] ## the JSON data to "remember"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
proc demo( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
    ## This is a demonstration handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
    ##
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
    ## It accepts and stores a JSON data structure
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
    ## on POST, and returns it on GET.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
    # Create a response object for the current request.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
    var response = request.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   251
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
    case request.meth
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
    # For GET requests, display the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
    of "GET":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
        if request[ "Accept" ].match( re(".*text/(html|plain).*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   258
            response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
            response.body   = "Hi there.  POST some JSON to me and I'll remember it.\n\n" & $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
            response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
        elif request[ "Accept" ].match( re("application/json") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
            response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   264
            response.body   = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
            response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
        else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
            response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   270
    # Overwrite the current JSON structure.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   271
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
    of "POST":
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
        if request[ "Content-Type" ].match( re(".*application/json.*") ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
            try:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   275
                data = request.body.parse_json
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
                response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   277
                response[ "Content-Type" ] = "application/json"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
                response.body = $( data )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
            except:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
                response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
                response[ "Content-Type" ] = "text/plain"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
                response.body = request.body
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   284
        else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   285
            response.body   = "I only accept valid JSON strings."
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
            response.status = HTTP_BAD_REQUEST
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
    else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
        response.status = HTTP_NOT_ACCEPTABLE
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
    return response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   292
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   294
# Attach the proc reference to the handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
handler.action = demo
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   296
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
# Start 'er up!
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
handler.run
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299
            </pre>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   300
            </div>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   301
        </div>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
    </body>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
</html>
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
    """
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
    HTTP_CONTINUE*                      = 100
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
    HTTP_SWITCHING_PROTOCOLS*           = 101
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
    HTTP_PROCESSING*                    = 102
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
    HTTP_OK*                            = 200
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
    HTTP_CREATED*                       = 201
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
    HTTP_ACCEPTED*                      = 202
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
    HTTP_NON_AUTHORITATIVE*             = 203
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
    HTTP_NO_CONTENT*                    = 204
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
    HTTP_RESET_CONTENT*                 = 205
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
    HTTP_PARTIAL_CONTENT*               = 206
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   316
    HTTP_MULTI_STATUS*                  = 207
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
    HTTP_MULTIPLE_CHOICES*              = 300
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
    HTTP_MOVED_PERMANENTLY*             = 301
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
    HTTP_MOVED*                         = 301
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
    HTTP_MOVED_TEMPORARILY*             = 302
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
    HTTP_REDIRECT*                      = 302
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
    HTTP_SEE_OTHER*                     = 303
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
    HTTP_NOT_MODIFIED*                  = 304
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
    HTTP_USE_PROXY*                     = 305
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
    HTTP_TEMPORARY_REDIRECT*            = 307
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
    HTTP_BAD_REQUEST*                   = 400
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
    HTTP_AUTH_REQUIRED*                 = 401
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   328
    HTTP_UNAUTHORIZED*                  = 401
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   329
    HTTP_PAYMENT_REQUIRED*              = 402
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
    HTTP_FORBIDDEN*                     = 403
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
    HTTP_NOT_FOUND*                     = 404
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
    HTTP_METHOD_NOT_ALLOWED*            = 405
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
    HTTP_NOT_ACCEPTABLE*                = 406
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
    HTTP_PROXY_AUTHENTICATION_REQUIRED* = 407
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
    HTTP_REQUEST_TIME_OUT*              = 408
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   336
    HTTP_CONFLICT*                      = 409
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   337
    HTTP_GONE*                          = 410
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   338
    HTTP_LENGTH_REQUIRED*               = 411
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   339
    HTTP_PRECONDITION_FAILED*           = 412
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
    HTTP_REQUEST_ENTITY_TOO_LARGE*      = 413
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
    HTTP_REQUEST_URI_TOO_LARGE*         = 414
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   342
    HTTP_UNSUPPORTED_MEDIA_TYPE*        = 415
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   343
    HTTP_RANGE_NOT_SATISFIABLE*         = 416
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
    HTTP_EXPECTATION_FAILED*            = 417
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
    HTTP_UNPROCESSABLE_ENTITY*          = 422
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
    HTTP_LOCKED*                        = 423
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
    HTTP_FAILED_DEPENDENCY*             = 424
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
    HTTP_UPGRADE_REQUIRED*              = 426
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
    HTTP_RECONDITION_REQUIRED*          = 428
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
    HTTP_TOO_MANY_REQUESTS*             = 429
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
    HTTP_REQUEST_HEADERS_TOO_LARGE*     = 431
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
    HTTP_SERVER_ERROR*                  = 500
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
    HTTP_NOT_IMPLEMENTED*               = 501
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
    HTTP_BAD_GATEWAY*                   = 502
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
    HTTP_SERVICE_UNAVAILABLE*           = 503
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
    HTTP_GATEWAY_TIME_OUT*              = 504
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
    HTTP_VERSION_NOT_SUPPORTED*         = 505
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
    HTTP_VARIANT_ALSO_VARIES*           = 506
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
    HTTP_INSUFFICIENT_STORAGE*          = 507
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
    HTTP_NOT_EXTENDED*                  = 510
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   361
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   362
    HTTPCODE = {
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   363
        HTTP_CONTINUE:                      ( desc: "Continue",                        label: "CONTINUE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
        HTTP_SWITCHING_PROTOCOLS:           ( desc: "Switching Protocols",             label: "SWITCHING_PROTOCOLS" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
        HTTP_PROCESSING:                    ( desc: "Processing",                      label: "PROCESSING" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
        HTTP_OK:                            ( desc: "OK",                              label: "OK" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
        HTTP_CREATED:                       ( desc: "Created",                         label: "CREATED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   368
        HTTP_ACCEPTED:                      ( desc: "Accepted",                        label: "ACCEPTED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
        HTTP_NON_AUTHORITATIVE:             ( desc: "Non-Authoritative Information",   label: "NON_AUTHORITATIVE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
        HTTP_NO_CONTENT:                    ( desc: "No Content",                      label: "NO_CONTENT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
        HTTP_RESET_CONTENT:                 ( desc: "Reset Content",                   label: "RESET_CONTENT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
        HTTP_PARTIAL_CONTENT:               ( desc: "Partial Content",                 label: "PARTIAL_CONTENT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
        HTTP_MULTI_STATUS:                  ( desc: "Multi-Status",                    label: "MULTI_STATUS" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   374
        HTTP_MULTIPLE_CHOICES:              ( desc: "Multiple Choices",                label: "MULTIPLE_CHOICES" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
        HTTP_MOVED_PERMANENTLY:             ( desc: "Moved Permanently",               label: "MOVED_PERMANENTLY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
        HTTP_MOVED:                         ( desc: "Moved Permanently",               label: "MOVED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
        HTTP_MOVED_TEMPORARILY:             ( desc: "Found",                           label: "MOVED_TEMPORARILY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
        HTTP_REDIRECT:                      ( desc: "Found",                           label: "REDIRECT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
        HTTP_SEE_OTHER:                     ( desc: "See Other",                       label: "SEE_OTHER" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
        HTTP_NOT_MODIFIED:                  ( desc: "Not Modified",                    label: "NOT_MODIFIED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
        HTTP_USE_PROXY:                     ( desc: "Use Proxy",                       label: "USE_PROXY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
        HTTP_TEMPORARY_REDIRECT:            ( desc: "Temporary Redirect",              label: "TEMPORARY_REDIRECT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
        HTTP_BAD_REQUEST:                   ( desc: "Bad Request",                     label: "BAD_REQUEST" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   384
        HTTP_AUTH_REQUIRED:                 ( desc: "Authorization Required",          label: "AUTH_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
        HTTP_UNAUTHORIZED:                  ( desc: "Authorization Required",          label: "UNAUTHORIZED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
        HTTP_PAYMENT_REQUIRED:              ( desc: "Payment Required",                label: "PAYMENT_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
        HTTP_FORBIDDEN:                     ( desc: "Forbidden",                       label: "FORBIDDEN" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
        HTTP_NOT_FOUND:                     ( desc: "Not Found",                       label: "NOT_FOUND" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
        HTTP_METHOD_NOT_ALLOWED:            ( desc: "Method Not Allowed",              label: "METHOD_NOT_ALLOWED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
        HTTP_NOT_ACCEPTABLE:                ( desc: "Not Acceptable",                  label: "NOT_ACCEPTABLE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   391
        HTTP_PROXY_AUTHENTICATION_REQUIRED: ( desc: "Proxy Authentication Required",   label: "PROXY_AUTHENTICATION_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   392
        HTTP_REQUEST_TIME_OUT:              ( desc: "Request Time-out",                label: "REQUEST_TIME_OUT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
        HTTP_CONFLICT:                      ( desc: "Conflict",                        label: "CONFLICT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
        HTTP_GONE:                          ( desc: "Gone",                            label: "GONE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
        HTTP_LENGTH_REQUIRED:               ( desc: "Length Required",                 label: "LENGTH_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
        HTTP_PRECONDITION_FAILED:           ( desc: "Precondition Failed",             label: "PRECONDITION_FAILED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   397
        HTTP_REQUEST_ENTITY_TOO_LARGE:      ( desc: "Request Entity Too Large",        label: "REQUEST_ENTITY_TOO_LARGE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   398
        HTTP_REQUEST_URI_TOO_LARGE:         ( desc: "Request-URI Too Large",           label: "REQUEST_URI_TOO_LARGE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   399
        HTTP_UNSUPPORTED_MEDIA_TYPE:        ( desc: "Unsupported Media Type",          label: "UNSUPPORTED_MEDIA_TYPE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   400
        HTTP_RANGE_NOT_SATISFIABLE:         ( desc: "Requested Range Not Satisfiable", label: "RANGE_NOT_SATISFIABLE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   401
        HTTP_EXPECTATION_FAILED:            ( desc: "Expectation Failed",              label: "EXPECTATION_FAILED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   402
        HTTP_UNPROCESSABLE_ENTITY:          ( desc: "Unprocessable Entity",            label: "UNPROCESSABLE_ENTITY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
        HTTP_LOCKED:                        ( desc: "Locked",                          label: "LOCKED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
        HTTP_FAILED_DEPENDENCY:             ( desc: "Failed Dependency",               label: "FAILED_DEPENDENCY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   405
        HTTP_UPGRADE_REQUIRED:              ( desc: "Upgrade Required",                label: "UPGRADE_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   406
        HTTP_RECONDITION_REQUIRED:          ( desc: "Precondition Required",           label: "RECONDITION_REQUIRED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   407
        HTTP_TOO_MANY_REQUESTS:             ( desc: "Too Many Requests",               label: "TOO_MANY_REQUESTS" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   408
        HTTP_REQUEST_HEADERS_TOO_LARGE:     ( desc: "Request Headers too Large",       label: "REQUEST_HEADERS_TOO_LARGE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   409
        HTTP_SERVER_ERROR:                  ( desc: "Internal Server Error",           label: "SERVER_ERROR" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
        HTTP_NOT_IMPLEMENTED:               ( desc: "Method Not Implemented",          label: "NOT_IMPLEMENTED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
        HTTP_BAD_GATEWAY:                   ( desc: "Bad Gateway",                     label: "BAD_GATEWAY" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   412
        HTTP_SERVICE_UNAVAILABLE:           ( desc: "Service Temporarily Unavailable", label: "SERVICE_UNAVAILABLE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
        HTTP_GATEWAY_TIME_OUT:              ( desc: "Gateway Time-out",                label: "GATEWAY_TIME_OUT" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
        HTTP_VERSION_NOT_SUPPORTED:         ( desc: "HTTP Version Not Supported",      label: "VERSION_NOT_SUPPORTED" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
        HTTP_VARIANT_ALSO_VARIES:           ( desc: "Variant Also Negotiates",         label: "VARIANT_ALSO_VARIES" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   416
        HTTP_INSUFFICIENT_STORAGE:          ( desc: "Insufficient Storage",            label: "INSUFFICIENT_STORAGE" ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
        HTTP_NOT_EXTENDED:                  ( desc: "Not Extended",                    label: "NOT_EXTENDED" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   418
    }.toTable
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   419
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   420
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   421
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   422
proc newM2Handler*( id: string, req_sock: string, res_sock: string ): M2Handler =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
    ## Instantiate a new `M2Handler` object.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
    ## The `id` should match the app ID in the Mongrel2 config,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
    ## `req_sock` is the ZMQ::PULL socket Mongrel2 sends client request
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   426
    ## data on, and `res_sock` is the ZMQ::PUB socket Mongrel2 subscribes
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   427
    ## to.  Nothing is put into action until the run() method is invoked.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   428
    new( result )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   429
    result.handler_id    = id
3
ecde1a332692 I'm not sure how I missed this one. Ssssh. Nothing to see here.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   430
    result.request_sock  = zmq.connect( req_sock, PULL )
ecde1a332692 I'm not sure how I missed this one. Ssssh. Nothing to see here.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   431
    result.response_sock = zmq.connect( res_sock, PUB )
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   432
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   433
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   434
proc parse_request( request: string ): M2Request =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   435
    ## Parse a message `request` string received from Mongrel2,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   436
    ## return it as a M2Request object.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   437
    var
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   438
        reqstr     = request.split( ' ' )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   439
        rest       = ""
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
        req_tnstr: TNetstringNode
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   441
        headers:   JsonNode
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   442
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   443
    new( result )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   444
    result.sender_id = reqstr[ 0 ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   445
    result.conn_id   = reqstr[ 1 ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   446
    result.path      = reqstr[ 2 ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   447
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   448
    # There must be a better way to join() a seq...
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   449
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   450
    for i in 3 .. reqstr.high:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   451
        rest = rest & reqstr[ i ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   452
        if i < reqstr.high: rest = rest & ' '
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   453
    reqtnstr    = rest.parse_tnetstring
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   454
    result.body = reqtnstr.extra.parse_tnetstring.getStr("")
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   455
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   456
    # Pull Mongrel2 control headers into the request object.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   457
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   458
    headers = reqtnstr.getStr.parse_json
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   459
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   460
    if headers.has_key( "METHOD" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   461
        result.meth = headers[ "METHOD" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   462
        headers.delete( "METHOD" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   463
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   464
    if headers.has_key( "PATTERN" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   465
        result.pattern = headers[ "PATTERN" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   466
        headers.delete( "PATTERN" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   467
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   468
    if headers.has_key( "REMOTE_ADDR" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   469
        result.remote_addr = headers[ "REMOTE_ADDR" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   470
        headers.delete( "REMOTE_ADDR" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   471
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
    if headers.has_key( "URI" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   473
        result.uri = headers[ "URI" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   474
        headers.delete( "URI" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   475
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   476
    if headers.has_key( "URL_SCHEME" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   477
        result.scheme = headers[ "URL_SCHEME" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   478
        headers.delete( "URL_SCHEME" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   479
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   480
    if headers.has_key( "VERSION" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   481
        result.version = headers[ "VERSION" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   482
        headers.delete( "VERSION" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   483
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   484
    if headers.has_key( "QUERY" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   485
        result.query = headers[ "QUERY" ].getStr
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   486
        headers.delete( "QUERY" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   487
    
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   488
    # Remaining headers are client supplied.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   489
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   490
    result.headers = @[]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   491
    for key, val in headers:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   492
        result.headers.add( ( key, val.getStr ) )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   493
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   494
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   495
proc response*( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   496
    ## Instantiate a new `M2Response`, paired from an `M2Request`.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   497
    new( result )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   498
    result.sender_id = request.sender_id
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   499
    result.conn_id   = request.conn_id
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   500
    result.headers   = @[]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   501
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   502
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   503
proc `[]`*( request: M2Request, label: string ): string =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   504
    ## Defer to the underlying headers tuple array.  Lookup is case-insensitive.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   505
    for header in request.headers:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   506
        if cmpIgnoreCase( label, header.name ) == 0:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   507
            return header.value
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   508
    return ""
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   509
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   510
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   511
proc is_disconnect*( request: M2Request ): bool =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   512
    ## Returns true if this is a Mongrel2 disconnect request.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   513
    if ( request.path == "@*" and request.meth == "JSON") :
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   514
        var body = request.body.parseJson
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   515
        if ( body.has_key( "type" ) and body[ "type" ].getStr == "disconnect" ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   516
            return true
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   517
    return false
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   518
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   519
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   520
proc `[]`*( response: M2Response, label: string ): string =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   521
    ## Defer to the underlying headers tuple array.  Lookup is case-insensitive.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   522
    for header in response.headers:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
        if cmpIgnoreCase( label, header.name ) == 0:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   524
            return header.value
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   525
    return ""
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   526
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   527
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   528
proc `[]=`*( response: M2Response, name: string, value: string ) =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   529
    ## Set a header on the response.  Duplicates are replaced.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   530
    var new_headers: seq[ tuple[name: string, value: string] ] = @[]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   531
    for header in response.headers:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   532
        if cmpIgnoreCase( name, header.name ) != 0:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   533
            new_headers.add( header )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   534
    response.headers = new_headers
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   535
    response.headers.add( (name, value) )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   537
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   538
proc add_header*( response: M2Response, name: string, value: string ) =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   539
    ## Adds a header to the response.  Duplicates are ignored.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   540
    response.headers.add( (name, value) )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   541
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   542
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   543
proc extend*( response: M2Response, filter: string ) =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   544
    ## Set a response as extended.  This means different things depending on
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   545
    ## the Mongrel2 filter in use.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   546
    response.extended = filter
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   547
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   548
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   549
proc add_extended_data*( response: M2Response, data: varargs[string, `$`] ) =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   550
    ## Attach filter arguments to the extended response.  Arguments should
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   551
    ## be coercible into strings.
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   552
    # if isNil( response.ex_data ): response.ex_data = @[]
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   553
    # response.ex_data = @[]
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   554
    for arg in data:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   555
        response.ex_data.add( arg )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   556
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   557
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   558
proc is_extended*( response: M2Response ): bool =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   559
    ## Predicate method to determine if a response is extended.
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   560
    return not ( response.extended == "" )
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   561
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   562
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   563
proc broadcast*[T]( response: M2Response, ids: openarray[T] ) =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   564
    ## Send the response to multiple backend client IDs.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   565
    assert( ids.len <= MAX_CLIENT_BROADCAST, "Exceeded client broadcast maximum" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   566
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   567
    response.conn_id = $( ids[0] )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   568
    for i in 1 .. ids.high:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   569
        if i <= ids.high: response.conn_id = response.conn_id & ' '
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   570
        response.conn_id = response.conn_id & $( ids[i] )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   571
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   572
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   573
proc format( response: M2Response ): string =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   574
    ## Format an `M2Response` object for Mongrel2.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   575
    var conn_id: string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   576
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   577
    # Mongrel2 extended response.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   578
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   579
    if response.is_extended:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   580
        conn_id = newTNetstringString( "X " & response.conn_id ).dump_tnetstring
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   581
        result = response.sender_id & ' ' & conn_id
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   582
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   583
        # 1st argument is the filter name.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   584
        #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   585
        var tnet_array = newTNetstringArray()
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   586
        tnet_array.add( newTNetstringString(response.extended) )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   587
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   588
        # rest are the filter arguments, if any.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   589
        #
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   590
        if response.is_extended:
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   591
            for data in response.ex_data:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   592
                tnet_array.add( newTNetstringString(data) )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   593
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   594
        result = result & ' ' & tnet_array.dump_tnetstring
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   595
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   596
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   597
    else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   598
        # Regular HTTP request/response cycle.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   599
        #
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   600
        if response.body == "":
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   601
            response.body = HTTPCODE[ response.status ].desc
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   602
            response[ "Content-Length" ] = $( response.body.len )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   603
        else:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   604
            response[ "Content-Length" ] = $( response.body.len )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   605
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   606
        let code = "$1 $2" % [ $(response.status), HTTPCODE[response.status].label ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   607
        conn_id  = newTNetstringString( response.conn_id ).dump_tnetstring
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   608
        result   = response.sender_id & ' ' & conn_id
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   609
        result   = result & " HTTP/1.1 " & code & CRLF
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   610
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   611
        for header in response.headers:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   612
            result = result & header.name & ": " & header.value & CRLF
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   613
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   614
        result = result & CRLF & response.body
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   615
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   616
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   617
proc handle_default( request: M2Request ): M2Response =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   618
    ## This is the default handler, if the caller didn't install one.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   619
    result                   = request.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   620
    result[ "Content-Type" ] = "text/html"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   621
    result.body              = DEFAULT_CONTENT
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   622
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   623
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   624
proc handle_disconnect( request: M2Request ): void =
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   625
    ## This is the default disconnect handler, if the caller didn't install one.
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   626
    return
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   627
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   628
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   629
proc run*( handler: M2Handler ) {. noreturn .} =
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   630
    ## Enter the request loop conversation with Mongrel2.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   631
    ## If an action() proc is attached, run that to generate
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   632
    ## a response.  Otherwise, run the default.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   633
    while true:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   634
        var
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   635
            request:  M2Request
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   636
            response: M2Response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   637
            info:     string
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   638
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   639
        request = parse_request( handler.request_sock.receive ) # block, waiting for next request
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   640
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   641
        # Ignore disconnects unless there's a separate
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   642
        # disconnect_action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   643
        #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   644
        if request.is_disconnect:
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   645
            if isNil( handler.disconnect_action ):
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   646
                handler.disconnect_action = handle_disconnect
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   647
            handler.disconnect_action( request )
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   648
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   649
        # Defer regular response content to the handler action.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   650
        #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   651
        if isNil( handler.action ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   652
            handler.action = handle_default
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   653
        response = handler.action( request )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   654
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   655
        if response.status == 0: response.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   656
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   657
        if defined( testing ):
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   658
            echo "REQUEST:\n",  repr(request)
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   659
            echo "RESPONSE:\n", repr(response)
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   660
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   661
        info = "$1 $2 $3" % [
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   662
            request.remote_addr,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   663
            request.meth,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   664
            request.uri
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   665
        ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   666
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   667
        echo "$1: $2 --> $3 $4" % [
6
d49437ff8f2f Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   668
            $(get_time()),
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   669
            info,
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   670
            $( response.status ),
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   671
            HTTPCODE[ response.status ].label
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   672
        ]
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   673
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   674
        handler.response_sock.send( response.format )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   675
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   676
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   677
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   678
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   679
# Tests!
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   680
#
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   681
when isMainModule:
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   682
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   683
    var reqstr = """host 33 /hosts 502:{"PATH":"/hosts","x-forwarded-for":"10.3.0.75","cache-control":"max-age=0","accept-language":"en-US,en;q=0.5","connection":"keep-alive","accept-encoding":"gzip, deflate","dnt":"1","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","user-agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0","host":"hotsoup.sunset.laika.com:8080","METHOD":"GET","VERSION":"HTTP/1.1","URI":"/hosts","PATTERN":"/hosts","URL_SCHEME":"http","REMOTE_ADDR":"10.3.0.75"},0:,"""
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   684
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   685
    var req  = parse_request( reqstr )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   686
    var dreq = parse_request( """host 1234 @* 17:{"METHOD":"JSON"},21:{"type":"disconnect"},""" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   687
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   688
    # Request parsing.
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   689
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   690
    doAssert( req.sender_id          == "host" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   691
    doAssert( req.conn_id            == "33" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   692
    doAssert( req.path               == "/hosts" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   693
    doAssert( req.remote_addr        == "10.3.0.75" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   694
    doAssert( req.scheme             == "http" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   695
    doAssert( req.meth               == "GET" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   696
    doAssert( req["DNT"]             == "1" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   697
    doAssert( req["X-Forwarded-For"] == "10.3.0.75" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   698
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   699
    doAssert( req.is_disconnect == false )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   700
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   701
    var res = req.response
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   702
    res.status = HTTP_OK
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   703
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   704
    doAssert( res.sender_id == req.sender_id )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   705
    doAssert( res.conn_id == req.conn_id )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   706
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   707
    # Response headers
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   708
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   709
    res.add_header( "alright", "yep" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   710
    res[ "something" ] = "nope"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   711
    res[ "something" ] = "yep"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   712
    doAssert( res["alright"]   == "yep" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   713
    doAssert( res["something"] == "yep" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   714
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   715
    # Client broadcasts
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   716
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   717
    res.broadcast([ 1, 2, 3, 4 ])
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   718
    doAssert( res.conn_id == "1 2 3 4" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   719
    doAssert( res.format == "host 7:1 2 3 4, HTTP/1.1 200 OK\r\nalright: yep\r\nsomething: yep\r\nContent-Length: 2\r\n\r\nOK" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   720
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   721
    # Extended replies
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   722
    #
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   723
    doAssert( res.is_extended == false )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   724
    res.extend( "sendfile" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   725
    doAssert( res.is_extended == true )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   726
    doAssert( res.format == "host 9:X 1 2 3 4, 11:8:sendfile,]" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   727
    res.add_extended_data( "arg1", "arg2" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   728
    res.add_extended_data( "arg3" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   729
    doAssert( res.format == "host 9:X 1 2 3 4, 32:8:sendfile,4:arg1,4:arg2,4:arg3,]" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   730
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   731
    doAssert( dreq.is_disconnect == true )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   732
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   733
    # Automatic body if none is specified
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   734
    #
4
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   735
    res.extended = ""
ffb8b9920057 Re-arrange for use with nimble, updates for nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   736
    res.body     = ""
0
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   737
    res.status   = HTTP_CREATED
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   738
    discard res.format
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   739
    doAssert( res.body == "Created" )
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   740
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   741
    echo "* Tests passed!"
f480e159f575 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   742