Skip to content

analyze request

Serge edited this page Oct 1, 2021 · 6 revisions

The realtime stickynotes app is responsible for three types of requests:

  • static files (HTML, javascript, CSS, etc)
  • websocket initialization
  • the websocket request itself ** we are asking Mochiweb to extract the request method:
http_handler(Request) -> 
    RequestMethod = mochiweb_request:get(method, Request),
    process_request(RequestMethod, Request).

we are interested only in GET requests:

process_request('GET', Request) ->
    IsWebsocketRequest = is_a_websocket_request(Request),
    handle_request(IsWebsocketRequest, Request).

we need to know if the client starts a new websocket session:

  • does the request have an Upgrade header?
  • if it does - is its value equal to websocket?
is_a_websocket_request(Request) ->
    Upgrade = mochiweb_request:get_header_value("Upgrade", Request),
    Upgrade =/= undefined
        andalso string:to_lower(Upgrade) =:= "websocket".

our server can potentially serve different applications, so we provide a route:

handle_request(?WEBSOCKET_REQUEST, Request) ->
    route(Request);

to serve a file we call a dedicated function:

handle_request(?REGULAR_REQUEST, Request) ->
    serve_file(Request).    

Clone this wiki locally