Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue while processing HEAD and GET request in rails 3

Currently we are facing issue for processing the HEAD and GET request. Let me explain the detailed scenario

We have integrated inbound and outbound SMS facility in our application.

But from last 2-3 months we are getting 2-3 times GET request from the SMS service provider and it is affecting on our system.

After long discussion with SMS service provider, they are saying "Both Head and Get requests are handled similarly from your end"

I also referred this link. You can find respective logs at this link

So can any one suggest how to resolve this issue.

EDIT After research we found that we are getting all the parameters in both HEAD and GET request because of this server is processing it.

like image 540
I-am-simple-user Avatar asked Nov 08 '22 23:11

I-am-simple-user


1 Answers

I think the problem might be the ActionDispatch::Head middleware. Part of that is following code:

def call(env)
  if env["REQUEST_METHOD"] == "HEAD"
    env["REQUEST_METHOD"] = "GET"
    env["rack.methodoverride.original_method"] = "HEAD"
    status, headers, _ = @app.call(env)
    [status, headers, []]
  else
    @app.call(env)
  end
end

So essentially the middleware changes the request-method before the router even gets the request. If you want your router to handle the difference between HEAD and GET requests, you can remove the middleware by adding

config.middleware.delete "ActionDispatch::Head"

to your application.rb

Otherwise you should be able to access that variable in your controller like this:

if request.env["rack.methodoverride.original_method"]=='HEAD'
  #do head processing here
  head :ok, :additional_header => 'value'
else
  #do get processing here
end

If you worry about performance, I suggest writing your own middleware to handle these requests. Railscasts has a few good tutorials on this.

Also note, that other middlewares, like Rack::Cache might interfere in this process as well. So you should insert your middleware on top:

config.middleware.insert_before 0, "YourMiddleware"
like image 137
skahlert Avatar answered Nov 15 '22 06:11

skahlert