Class: Maze::Servlets::Servlet

Inherits:
BaseServlet
  • Object
show all
Defined in:
lib/maze/servlets/servlet.rb

Overview

Receives and parses the requests and payloads sent from the test fixture

Direct Known Subclasses

TraceServlet

Instance Method Summary collapse

Constructor Details

#initialize(server, request_type, schema = nil) ⇒ Servlet

Constructor

Parameters:

  • server (HTTPServer)

    WEBrick HTTPServer

  • request_type (Symbol)

    Request type that the servlet will receive

  • schema (Dictionary) (defaults to: nil)

    A ‘json-schema` describing the payload for POST requests



38
39
40
41
42
43
44
# File 'lib/maze/servlets/servlet.rb', line 38

def initialize(server, request_type, schema=nil)
  super server
  @request_type = request_type
  @schema = JSONSchemer.schema(schema) unless schema.nil?
  @aspecto_repeater = Maze::Repeaters::AspectoRepeater.new(@request_type)
  @bugsnag_repeater = Maze::Repeaters::BugsnagRepeater.new(@request_type)
end

Instance Method Details

#do_GET(request, _response) ⇒ Object

Logs an incoming GET WEBrick request.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • _response (HTTPResponse)

    The response to return



50
51
52
# File 'lib/maze/servlets/servlet.rb', line 50

def do_GET(request, _response)
  log_request(request)
end

#do_OPTIONS(request, response) ⇒ Object

Logs and returns a set of valid headers for this servlet.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



152
153
154
155
156
157
# File 'lib/maze/servlets/servlet.rb', line 152

def do_OPTIONS(request, response)
  super

  response.header['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
  response.status = Server.status_code('OPTIONS')
end

#do_POST(request, response) ⇒ Object

Logs and parses an incoming POST request. Parses ‘multipart/form-data` and `application/json` content-types. Parsed requests are added to the requests list.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/maze/servlets/servlet.rb', line 60

def do_POST(request, response)

  @aspecto_repeater.repeat request
  @bugsnag_repeater.repeat request

  # Turn the WEBrick HttpRequest into our internal HttpRequest delegate
  request = Maze::HttpRequest.new(request)

  content_type = request['Content-Type']
  if %r{^multipart/form-data; boundary=([^;]+)}.match(content_type)
    boundary = WEBrick::HTTPUtils::dequote($1)
    body = WEBrick::HTTPUtils.parse_form_data(request.body, boundary)
    hash = {
      body: body,
      request: request,
      response: response
    }
  else
    # "content-type" is assumed to be JSON (which mimics the behaviour of
    # the actual API). This supports browsers that can't set this header for
    # cross-domain requests (IE8/9)
    digests = check_digest request
    hash = {
      body: JSON.parse(request.body),
      request: request,
      response: response,
      digests: digests
    }
  end
  if @schema
    schema_errors = @schema.validate(hash[:body])
    hash[:schema_errors] = schema_errors.to_a
  end
  add_request(hash)

  # For the response, delaying if configured to do so
  response_delay_ms = Server.response_delay_ms
  if response_delay_ms.positive?
    $logger.info "Waiting #{response_delay_ms} milliseconds before responding"
    sleep response_delay_ms / 1000.0
  end

  set_response_header response.header
  response.status = post_status_code
rescue JSON::ParserError => e
  msg = "Unable to parse request as JSON: #{e.message}"
  if Maze.config.captured_invalid_requests.include? @request_type
    Bugsnag.notify e
    $logger.error msg
    Server.invalid_requests.add({
      reason: msg,
      request: request,
      body: request.body
    })
  else
    $logger.warn msg
  end
rescue StandardError => e
  msg = "Invalid #{@request_type} request: #{e.message}"
  if Maze.config.captured_invalid_requests.include? @request_type
    Bugsnag.notify e
    $logger.error msg
    Server.invalid_requests.add({
      invalid: true,
      reason: e.message,
      request: {
        request_uri: request.request_uri,
        header: request.header,
        body: request.inspect
      }
    })
  else
    $logger.warn msg
  end
end

#post_status_codeObject



136
137
138
139
140
141
142
# File 'lib/maze/servlets/servlet.rb', line 136

def post_status_code
  if [:errors, :sessions, :builds, :uploads, :sourcemaps, :traces].include? @request_type
    Server.status_code('POST')
  else
    200
  end
end

#set_response_header(header) ⇒ Object



144
145
146
# File 'lib/maze/servlets/servlet.rb', line 144

def set_response_header(header)
  header['Access-Control-Allow-Origin'] = '*'
end