Method: OpenC3::JsonDRb#process_request

Defined in:
lib/openc3/io/json_drb.rb

#process_request(request_data:, request_headers:, start_time:) ⇒ Object

Process the JSON request data, execute the method, and create a response.

Parameters:

  • The JSON encoded request

  • The requests headers sent with the request

  • The time when the initial request was received

Returns:

  • response_data, error_code [String, Integer/nil] The JSON encoded response and error code



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/openc3/io/json_drb.rb', line 256

def process_request(request_data:, request_headers:, start_time:)
  @request_count += 1
  begin
    request = JsonRpcRequest.from_json(request_data, request_headers)
    response = nil
    error_code = nil
    response_data = nil

    if (@method_whitelist and @method_whitelist.include?(request.method.downcase())) or
       (!@method_whitelist and !JsonRpcRequest::DANGEROUS_METHODS.include?(request.method.downcase()))
      begin
        if request.keyword_params
          result = @object.public_send(request.method.downcase().intern, *request.params, **request.keyword_params)
        else
          result = @object.public_send(request.method.downcase().intern, *request.params)
        end
        if request.id
          response = JsonRpcSuccessResponse.new(result, request.id)
        end
      rescue Exception => error
        # Filter out the framework stack trace (rails, rack, puma etc)
        lines = error.formatted.split("\n")
        i = lines.find_index { |row| row.include?('actionpack') || row.include?('activesupport') }
        Logger.error lines[0...i].join("\n")

        if request.id
          if NoMethodError === error
            error_code = JsonRpcError::ErrorCode::METHOD_NOT_FOUND
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, "Method not found", error), request.id
            )
          elsif ArgumentError === error
            error_code = JsonRpcError::ErrorCode::INVALID_PARAMS
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, "Invalid params", error), request.id
            )
          elsif AuthError === error
            error_code = JsonRpcError::ErrorCode::AUTH_ERROR
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, error.message, error), request.id
            )
          elsif ForbiddenError === error
            error_code = JsonRpcError::ErrorCode::FORBIDDEN_ERROR
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, error.message, error), request.id
            )
          elsif HazardousError === error
            error_code = JsonRpcError::ErrorCode::HAZARDOUS_ERROR
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, error.message, error), request.id
            )
          else
            error_code = JsonRpcError::ErrorCode::OTHER_ERROR
            response = JsonRpcErrorResponse.new(
              JsonRpcError.new(error_code, error.message, error), request.id
            )
          end
        end
      end
    else
      if request.id
        error_code = JsonRpcError::ErrorCode::OTHER_ERROR
        response = JsonRpcErrorResponse.new(
          JsonRpcError.new(error_code, "Cannot call unauthorized methods"), request.id
        )
      end
    end
    response_data = process_response(response, start_time) if response
    return response_data, error_code
  rescue => error
    error_code = JsonRpcError::ErrorCode::INVALID_REQUEST
    response = JsonRpcErrorResponse.new(JsonRpcError.new(error_code, "Invalid Request", error), nil)
    response_data = process_response(response, start_time)
    return response_data, error_code
  end
end