Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update common.js #744

Closed
wants to merge 1 commit into from
Closed

Update common.js #744

wants to merge 1 commit into from

Conversation

koolc
Copy link
Contributor

@koolc koolc commented Nov 25, 2014

Bugfix: outgoing url is wrong when the source url is 'http://a.b.c/d/??e/a.js,f/b.js,g/c.js?t=123456' (a combo url).

Bugfix: outgoing url is wrong when the source url is 'http://a.b.c/d/??e/a.js,f/b.js,g/c.js?t=123456' (a combo url).
@koolc
Copy link
Contributor Author

koolc commented Nov 25, 2014

When you merged the changed codes and update to a new version, please leave a message! Thank you!

@@ -155,7 +155,12 @@ common.urlJoin = function() {

// Only join the query string if it exists so we don't have trailing a '?'
// on every request
lastSegs[1] && retSegs.push(lastSegs[1]);
// Maybe there are many '?' in the url,
// such as 'http://a.b.c/d/??e/a.js,f/b.js,g/c.js?t=123456'(a combo request url),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even a valid url?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcrugzz
Copy link
Contributor

jcrugzz commented Nov 25, 2014

@koolc is there a failing test this is compensating for? To be fair this logic is still not 100% correct but this change doesn't look like the right fix.

@koolc
Copy link
Contributor Author

koolc commented Nov 26, 2014

@jcrugzz @samccone
This is a online url 'http://g.tbcdn.cn/??kissy/k/1.4.0/seed-min.js,kissy/k/1.4.0/base-min.js,kissy/k/1.4.0/json-min.js,kissy/k/1.4.0/dom/base-min.js,kissy/k/1.4.0/event/base-min.js,kissy/k/1.4.0/event/custom-min.js,kissy/k/1.4.0/event/dom/base-min.js,kissy/k/1.4.0/event/dom/focusin-min.js' on the Alibaba CDNs. You can take it
for test.

This is a combo url(This is a technical measures that multiple requests will be merged into one in order to reduce the number of requests), and '??' is a combo tag.

I mainly think of this case, so maybe the changed logic is still not 100% correct, hope you can revise it.

@jcrugzz
Copy link
Contributor

jcrugzz commented Nov 26, 2014

@koolc could you write a test case using this type of URL? I will look closer at the logic

@koolc
Copy link
Contributor Author

koolc commented Nov 27, 2014

A bin test file content:

#!/usr/bin/env node --harmony

/* ==========================================================================
   A Test Case for the method `common.urlJoin` of 'lib/http-proxy/common.js'
                                              - added by koolc on 2014.11.17
 ============================================================================
   - conditions:
     - the ingoing url is a combo url, such as 'http://g.tbcdn.cn/??kissy/
       k/1.4.0/seed-min.js,kissy/k/1.4.0/base-min.js,kissy/k/1.4.0/json-min
       .js,kissy/k/1.4.0/dom/base-min.js,kissy/k/1.4.0/event/base-min.js,
       kissy/k/1.4.0/event/custom-min.js,kissy/k/1.4.0/event/dom/base-min
       .js,kissy/k/1.4.0/event/dom/focusin-min.js'.

     - the host bind is 'g.tbcdn.cn 127.0.0.1'.

   - expectation:
     - the outgoing url is a whole url like origin url, except for the port,
       such as 'http://127.0.0.1:9000/??kissy/k/1.4.0/seed-min.js...'
 ========================================================================== */

var http = require('http');
var httpProxy = require('http-proxy');
var koa = require('koa');

// The proxy server
var proxy = httpProxy.createProxyServer();
http
    .createServer(function (req, res) {
        console.log('[proxy server] the path of the ingoing url: "' + req.url + '"');
        proxy.web(req, res, {
            target: {
                host: '127.0.0.1',
                port: 9000
            }
        }, function (err, req, res) {
            res.writeHead(500, {
                'Content-Type': 'text/plain'
            });
            res.end(err.message);
        });
    })
    .listen(80);

console.log('Proxy server started at ' + new Date());
console.log('Listening on http://127.0.0.1:80');
console.log('---------------------------------------------------------------');

// The web server
var app = koa();
app.use(function*(next){ // to validate the url value.
    // output path expected is "/??kissy/k/1.4.0/seed-min.js...focusin-min.js"
    console.log('[web   server] the path of the outgoing url: "' + this.url + '"'); // real output path: "/"
    yield next;
});

http.createServer(app.callback()).listen(9000);

console.log('Web server started at ' + new Date());
console.log('Listening on http://127.0.0.1:9000');
console.log('---------------------------------------------------------------');


console.log('The logs will output here. <Press Ctrl-C to quit>');


@jcrugzz
Copy link
Contributor

jcrugzz commented Nov 27, 2014

@koolc In terms of a test case, could you add a test case to test of the common.setupOutgoing function in here

@koolc
Copy link
Contributor Author

koolc commented Nov 27, 2014

I think the test case(exists many '?') below in the common.setupOutgoing function could enough test out the problem. The codes:

    it('should not modify the query string and hash string', function () {
      var outgoing = {};
      common.setupOutgoing(outgoing, {
        target: { path: '/forward' },
      }, { url: '/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=some#id12' });

      expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=some#id12');
    })

I have sent a pull request , you can see my changes.

// so to use `forEach`.
lastSegs.forEach(function (slice) {
retSegs.push(slice);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this below is better.

retSegs.push.apply(retSegs, lastSegs);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that change would be ideal, after that, this LGTM.

@koolc
Copy link
Contributor Author

koolc commented Nov 28, 2014

Excuse me, but I want to know when to support the change? My component will recently publish and it requires the 'http-proxy' module. So I expect the fresh version very much. Thank you!

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 1, 2014

@koolc sorry for the late reply, once that change you already suggested is done, this should be merged. If you have anymore URL test cases for this function, it would be awesome to get more tests around this.

@koolc
Copy link
Contributor Author

koolc commented Dec 1, 2014

Sorry, I didn't fully understand what you mean, but the previous test case modified by me has been provided at #746 last week, and I'm sorry I have the only one application scene to use the function, so it's difficult for me to get more tests around this. Though, even if no more test cases, I'm sure that the function must be wrong. So, at least so far, I think it's the most important to correct it and the test cases can be accumulated slowly. Don't you think so?

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 1, 2014

@koolc test cases are not required, I was just curious if you had anymore crazy URLs that could be sueful for testing ;). Just make the change for retSegs.push.apply(retSegs, lastSegs); and I will merge.

@koolc
Copy link
Contributor Author

koolc commented Dec 2, 2014

Thank you! I have seen the merged codes, but they doesn't seem to be completely right. Please see the message left by me on #748

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 2, 2014

@koolc replied

jcrugzz added a commit that referenced this pull request Dec 2, 2014
@jcrugzz jcrugzz closed this Dec 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants