in Education by
I have the following code (just as a test) and I want to create an HTTP proxy using EventMachine. The code below is an example on the es-proxy GitHub page. However, when I run this and open up a website that has a moderate amount of images, the images start loading incorrectly. What I mean by this is that some images are loaded twice or if I request my icon for the navigation bar, I instead get the profile picture. This is especially evident if I refresh the page a few times. It seems that the responses do not correspond to the matching request; causing everything to be jumbled. However, I'm not sure why this is. The code below seems simple enough for this to not be a problem. require 'rubygems' require 'em-proxy' require 'http/parser' # gem install http_parser.rb require 'uuid' # gem install uuid # > ruby em-proxy-http.rb # > curl --proxy localhost:9889 www.google.com host = "0.0.0.0" port = 9889 puts "listening on #{host}:#{port}..." Proxy.start(:host => host, :port => port) do |conn| @p = Http::Parser.new @p.on_headers_complete = proc do |h| session = UUID.generate puts "New session: #{session} (#{h.inspect})" host, port = h['Host'].split(':') conn.server session, :host => host, :port => (port || 80) conn.relay_to_servers @buffer @buffer = '' end @buffer = '' conn.on_connect do |data,b| puts [:on_connect, data, b].inspect end conn.on_data do |data| @buffer << data @p << data data end conn.on_response do |backend, resp| #puts [:on_response, backend, resp].inspect resp end conn.on_finish do |backend, name| puts [:on_finish, name].inspect end end Update I believe I have insight as to what is happening but, still no way of solving my problem. I am creating a server for each request and when I relay my requests I have multiple servers. Then in the on response I should only be returning the response if it is from the correct server. However, I don't have a way to correlate this as of yet. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here a proper response: Try removing every puts in the example so the main loop can concentrate on doing the actual network I/O, it works for me like that. I think there may be some kind of timeout playing behind this, maybe the client does not wait long enough for the full answer to come back while the server is stuck outputing text to the console. That's the downside of using an event reactor, you have to make sure nothing blocks it.

Related questions

0 votes
    When dealing with mobile clients it is very common to have multisecond delays during the transmission of HTTP ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am trying to start a local Kubernetes cluster using minikube start and getting the following error. Starting local ... this slow? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am building a plugin for a web application in java which is running on Tomcat and behind a apache ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am building a plugin for a web application in java which is running on Tomcat and behind a apache ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    What happens when Intercept is off? A.) The request will hit the server B.) The request will hit the proxy C.) Hit the proxy...
asked Apr 29, 2021 in Technology by JackTerrance
0 votes
    I've got a Node.js powered site that I'm running on Amazon Elastic Beanstalk. My Node.js app listens on port ... to my site via HTTPS. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Which object stores references to the request and response objects? (a) sessionContext (b) pageContext (c) HttpSession ... API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which object stores references to the request and response objects? (a) sessionContext (b) pageContext (c) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    I am trying develop first app in ionic and with codeigniter backend but facing Response for preflight has ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    I am trying to POST a HTML (contained in a file) to a URL using Wget like this: wget -O- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden. ... Selenium WebDriver? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Which of the following is a feature of the HTTP response? (a) Mandatory response body (b) Optional ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which among the below directives belonging to the Cache-Control header of HTTP response provide information to the server that the ... -revalidate 3. no-cache 4. None of the above...
asked Jun 24, 2021 in Technology by JackTerrance
0 votes
    Which directive of Cache Control Header of HTTP response provides indication to server to revalidate resource if max-age has passed?...
asked Nov 7, 2020 in Technology by JackTerrance
0 votes
    Which directive of Cache Control Header of HTTP response can set the time limit of caching?...
asked Nov 7, 2020 in Technology by JackTerrance
...