8000 Bug in Middleware Flow (only for HEAD Requests?) · Issue #100 · h2non/rocky · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Mar 16, 2022. It is now read-only.
This repository was archived by the owner on Mar 16, 2022. It is now read-only.
Bug in Middleware Flow (only for HEAD Requests?) #100
Open
@alixaxel

Description

@alixaxel

I believe I've found a bug in the middleware execution flow.

Consider the following simplified example:

const rocky = require('rocky');
const express = require('express');

let snafu = (port) => {
  let app = express();
  let proxy = rocky();
  let methods = ['head', 'get'];

  if (port !== 1337) {
    methods.reverse();
  }

  for (let route of ['/foo']) {
    for (let method of methods) {
      let middlewares = [];

      if (method === 'get') {
        middlewares.push((request, response, next) => {
          console.log(`GET on ${port}.`); return next();
        });
      } else if (method === 'head') {
        middlewares.push((request, response, next) => {
          console.log(`HEAD on ${port}.`); return next();
        });
      }

      proxy[method](route, middlewares).forward('http://localhost:3001');
    }
  }

  app.use(proxy.middleware());
  app.listen(port, () => {
    console.log(`Listening on port ${port}...`);
  });
};

snafu(1337);
snafu(1338); // --> buggy!

The above function will register two middlewares, one for GET and another one for HEAD requests.

The function is called twice, if the port is 1337 the order of the routes will be:

  1. HEAD /foo
  2. GET /foo

Otherwise (if the port is 1338), the order in which the routes will be defined is:

  1. GET /foo
  2. HEAD /foo

And this is the output of the 4 different cURL calls:

  • curl -I "http://localhost:1337/foo" = HEAD on 1337
  • curl -X GET "http://localhost:1337/foo" = GET on 1337
  • curl -I "http://localhost:1338/foo" = GET on 1338 wrong!
  • curl -X GET "http://localhost:1338/foo" = GET on 1338

Curiously enough, if I change the HEAD to a POST I get the expected behavior in all cases.

Is there a reason for this to happen?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0