Module: David::Server::Respond

Includes:
CoAP::Coding, Registry, Constants, Mapping, Utility
Included in:
David::Server
Defined in:
lib/david/server/respond.rb

Constant Summary

Constants included from Mapping

Mapping::HTTP_TO_COAP_CODES, Mapping::HTTP_TO_COAP_CODES_MINIMAL

Constants included from Constants

Constants::ASCII_8BIT, Constants::COAP_CBOR, Constants::COAP_DTLS, Constants::COAP_DTLS_ID, Constants::COAP_DTLS_NOSEC, Constants::COAP_MULTICAST, Constants::COAP_VERSION, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::CONTENT_TYPE_CBOR, Constants::CONTENT_TYPE_JSON, Constants::EMPTY_STRING, Constants::HTTP_ACCEPT, Constants::HTTP_CACHE_CONTROL, Constants::HTTP_CONTENT_LENGTH, Constants::HTTP_CONTENT_TYPE, Constants::HTTP_ETAG, Constants::HTTP_LOCATION, Constants::PATH_INFO, Constants::QUERY_STRING, Constants::RACK_ERRORS, Constants::RACK_INPUT, Constants::RACK_LOGGER, Constants::RACK_MULTIPROCESS, Constants::RACK_MULTITHREAD, Constants::RACK_RUN_ONCE, Constants::RACK_URL_SCHEME, Constants::RACK_URL_SCHEME_HTTP, Constants::RACK_VERSION, Constants::REMOTE_ADDR, Constants::REMOTE_PORT, Constants::REQUEST_METHOD, Constants::SCRIPT_NAME, Constants::SERVER_NAME, Constants::SERVER_PORT

Instance Method Summary collapse

Instance Method Details

#respond(exchange, env = nil) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/david/server/respond.rb', line 15

def respond(exchange, env = nil)
  block_enabled = @options[:Block] && exchange.get?

  if block_enabled
    # Fail if m set.
    if exchange.block.more && !exchange.multicast?
      return error(exchange, 4.05)
    end
  end

  return error(exchange, 5.05) if exchange.proxy?

  env ||= basic_env(exchange)

  if @options[:CBOR] && exchange.cbor?
    begin
      cbor = CBOR.load(exchange.message.payload)

      body = body_to_json(cbor)
      body = body.force_encoding(ASCII_8BIT) # Rack::Lint insisted...

      env[COAP_CBOR]      = cbor
      env[CONTENT_LENGTH] = body.bytesize.to_s
      env[CONTENT_TYPE]   = CONTENT_TYPE_JSON
      env[RACK_INPUT]     = StringIO.new(body)
    rescue EOFError, CBOR::MalformedFormatError
    end
  end

  code, headers, body = @app.call(env)

  # No error responses on multicast exchanges.
  return if exchange.multicast? && !(200..299).include?(code)

  ct = media_type_strip(headers[HTTP_CONTENT_TYPE])
  body = body_to_string(body)

  if @options[:CBOR] && ct == CONTENT_TYPE_JSON
    begin
      body = body_to_cbor(body)
      ct = CONTENT_TYPE_CBOR
    rescue JSON::ParserError
    end
  end

  # No response on exchange for non-existent block.
  return if block_enabled && !exchange.block.included_by?(body)

  cf    = CoAP::Registry.convert_content_format(ct)
  etag  = etag_to_coap(headers, 4)
  loc   = location_to_coap(headers)
  ma    = max_age_to_coap(headers)
  mcode = code_to_coap(code)

  # App returned cf different from accept
  return error(exchange, 4.06) if exchange.accept && exchange.accept != cf

  response = initialize_response(exchange, mcode)

  response.options[:content_format] = cf
  response.options[:etag] = etag
  response.options[:location_path] = loc unless loc.nil?
  response.options[:max_age] = ma.to_i unless ma.nil?

  if @options[:Observe] && handle_observe(exchange, env, etag)
    response.options[:observe] = 0
  end

  if block_enabled
    block = exchange.block.dup
    block.set_more!(body)

    response.payload = block.chunk(body)
    response.options[:block2] = block.encode
  else
    response.payload = body
  end

  [response, {}]
end