Class: WebSvr::RequestParams

Inherits:
Object
  • Object
show all
Defined in:
lib/web_svr/request_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, log) ⇒ RequestParams

Set up the web server.



26
27
28
29
# File 'lib/web_svr/request_params.rb', line 26

def initialize( engine, log ) 
  @engine = engine
  @log = log
end

Instance Attribute Details

#body_binaryObject (readonly)

Returns the value of attribute body_binary.



17
18
19
# File 'lib/web_svr/request_params.rb', line 17

def body_binary
  @body_binary
end

#body_paramsObject (readonly)

Returns the value of attribute body_params.



17
18
19
# File 'lib/web_svr/request_params.rb', line 17

def body_params
  @body_params
end

#idObject

Returns the value of attribute id.



16
17
18
# File 'lib/web_svr/request_params.rb', line 16

def id
  @id
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



17
18
19
# File 'lib/web_svr/request_params.rb', line 17

def query_params
  @query_params
end

#route_paramsObject

Returns the value of attribute route_params.



16
17
18
# File 'lib/web_svr/request_params.rb', line 16

def route_params
  @route_params
end

Instance Method Details

#check_authenticity_token(engine) ⇒ Object

Check the authenticity token if it is present. Returns true if it is present and valid, and also if it is not present. Returns false if it is present but not valid.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/web_svr/request_params.rb', line 120

def check_authenticity_token engine
  auth_token = @query_params[ Gloo::Objs::CsrfToken::AUTHENTICITY_TOKEN ]
  if auth_token
    session_id = engine.running_app.obj&.session&.get_session_id
    return false unless session_id

    return Gloo::Objs::CsrfToken.valid_csrf_token?( session_id, auth_token )
  end

  return true
end

#get_body_method_override(orig_method) ⇒ Object

Check the body to see if there is a PATCH or a PUT in the method override.



141
142
143
144
145
146
# File 'lib/web_svr/request_params.rb', line 141

def get_body_method_override orig_method
  if @body_params[ '_method' ]
    return @body_params[ '_method' ].upcase
  end
  return orig_method
end

#init_body_params(body) ⇒ Object

Detect the parameters from the body of the request.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/web_svr/request_params.rb', line 50

def init_body_params body
  if body && body.length > 0
    # if body is binary, then it is not a query string
    begin
      @body_params = Rack::Utils.parse_query body
    rescue => exception
      init_multipart body 
    end
  else
    @body_params = {} 
  end
end

#init_multipart(body) ⇒ Object

Set the body to a binary file.

TODO: find a lib or method to handle this. This is very rough and will need to be fixed.



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
# File 'lib/web_svr/request_params.rb', line 69

def init_multipart body
  begin
    # puts "*********** first lines: *********** "
    # body.lines[0..3].each { |line| puts line }
    # puts "*********** last lines: *********** "
    # body.lines.last(5).each { |line| puts line }
    # puts "************************************"

    # boundary = body.lines.first
    # puts "boundary: #{boundary}"

    header = body.lines[1..3].join
    # puts "header: #{header}"

    footer = body.lines.last(5).join
    # puts "footer: #{footer}"

    binary_data = body.lines[4..-6].join
    # puts "binary_data length: #{binary_data.length}"
    # puts "binary first line: #{binary_data.lines.first}"
    # puts "binary last line: #{binary_data.lines.last}"

    i = header.lines.first.index( 'filename=' )
    filename = header.lines.first[ i+10..-4 ]
    content_type = header.lines.second[14..-3]
    # puts "filename: #{filename}"
    # puts "content_type: #{content_type}"

    @body_binary = body
    @body_params = {}
    @body_params[ 'content_type' ] = content_type
    @body_params[ 'file_name' ] = filename
    @body_params[ 'file_size' ] = binary_data.length
    @body_params[ 'file_data' ] = binary_data
  rescue => ex
    @engine.log_exception ex
    # @log.error "Error parsing multipart request", ex
  end
end

#init_query_params(query_string) ⇒ Object

Detect the parameters from query string.



39
40
41
42
43
44
45
# File 'lib/web_svr/request_params.rb', line 39

def init_query_params query_string
  if query_string
    @query_params = Rack::Utils.parse_query( query_string )
  else
    @query_params = {} 
  end
end

#log_id_keysObject

Write the id and route params to the log.



174
175
176
177
178
179
180
181
182
# File 'lib/web_svr/request_params.rb', line 174

def log_id_keys
  return unless @log

  @log.info "--- ID Parameter: #{@id}" if @id

  if @route_params && ! @route_params.empty?
    @log.info "--- Route Parameters: #{@route_params}"
  end
end

#log_paramsObject

Write the querey and body params to the log.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/web_svr/request_params.rb', line 151

def log_params
  return unless @log

  if @query_params && ! @query_params.empty?
    @log.info "--- Query Parameters: #{@query_params}" 
  end

  if @body_params && ! @body_params.empty?
    if @body_params[ 'file_data' ]
      # exclude the file data from the params shown
      params = @body_params.dup
      params.delete( 'file_data' )
      params[ 'file_data' ] = '...'
      @log.info "--- Body Parameters: #{params}"
    else
      @log.info "--- Body Parameters: #{@body_params}"
    end
  end
end