title: HAProxy CORS OPTIONS header intercept setup
body: <p>With my NGinx setup I was able to intercept OPTIONS requests from ajax preflight and respond with the correct CORS headers and a 200 response so the request could continue onwards.  I am attempting to consolidate my frontend proxies into HAProxy and am having some issues getting this piece of the puzzle working.  </p>
  
  <p>My particular issue is that while I am able to add the proper CORS options when there is a server able to respond properly to an OPTIONS request, a few of the backends cannot handle/respond with a 405 error when the preflight request is issued.  My haproxy.cfg included the following lines for adding the headers:</p>
  
  <pre><code>capture request header origin len 128
  http-response add-header Access-Control-Allow-Origin %[capture.req.hdr(0)] if { capture.req.hdr(0) -m found }
  rspadd Access-Control-Allow-Credentials:\ true if { capture.req.hdr(0) -m found }
  rspadd Access-Control-Allow-Headers:\ Origin,\ X-Requested-With,\ Content-Type,\ Origin,\ User-Agent,\ If-Modified-Since,\ Cache-Control,\ Accept if { capture.req.hdr(0) -m found }
  rspadd Access-Control-Allow-Methods:\ GET,\ POST,\ PUT,\ DELETE,\ OPTIONS if { capture.req.hdr(0) -m found }
  rspadd Access-Control-Max-Age:\ 1728000 if { capture.req.hdr(0) -m found }
  </code></pre>
  
  <p>The solution given in: </p>
  
  <p><a href="https://stackoverflow.com/questions/26556838/how-to-send-a-response-with-haproxy-without-passing-the-request-to-web-servers">How to send a response with HAProxy without passing the request to web servers</a> works when you set all of the correct headers from a client's request, but is not dynamic which is not an ideal solution.</p>
  
  <p>Any help would be appreciated!</p>