phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

Custom HTTP header fields order

Open iddl opened this issue 12 years ago • 29 comments

According to RFC2616 (http://www.w3.org/Protocols/rfc2616/rfc2616.html)

the order in which header fields with differing field names are received is not significant

however, some implementations may pay particular attention to the order of these fields.

Are there any plans to support custom orderings ?

eg. Have "Connection: Keep-Alive" come before "Accept-Encoding: gzip".

A webpage property similar to this would probably work: page.headerFieldsOrder = ["Accept", "Accept-Language", "Host", "Connection"...];

Thanks

iddl avatar Dec 31 '13 00:12 iddl

What benefit does it have?

JamesMGreene avatar Dec 31 '13 06:12 JamesMGreene

It may not be a critical enhancement but it would likely improve the flexibility of the tool.

I have been fiddling around with phantomjs and CDNs and found out some services like Incapsula may be looking at the order of HTTP request headers other than values to determine the type of browser.

Here are the images of two GET requests, the first made by Firefox 26 and the second by phantomjs using customHeaders to mimic Firefox.

Firefox:

firefox

Phantomjs with customHeaders:

phantomjs

Below is the code I used to set the headers. Some of the field values may not be compatible, however, my goal was to get two identical HTTP responses from the server.

page.customHeaders = {
    "User-Agent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "Connection" : "keep-alive"
};

Given the same values for the headers I would expect identical responses, for some reason this doesn't happen. The order of the fields may make a difference.

Thoughts ?

iddl avatar Jan 02 '14 01:01 iddl

I also find this feature useful. Any plans to add it soon?

ghost avatar Apr 02 '14 00:04 ghost

:+1: +1

RobinDev avatar Nov 14 '14 13:11 RobinDev

+1 Without possibility to modify HTTP headers order it is impossible to fetch sites protected with Incapsula.

Kirzilla avatar Dec 15 '14 12:12 Kirzilla

+1

xckon avatar Jan 28 '15 19:01 xckon

+1

dufoli avatar Nov 23 '15 05:11 dufoli

I have successfully made PhantomJS fetch a page from an Incapsula-protected site (www.enjin.com), with modification of two files:

  1. qhttpnetworkrequest.cpp (fixed http header field ordering)
  2. webpage.cpp (renamed the phantom-callback object name, in order for the Incapsula js-test not finding it)

gist (including example script): https://gist.github.com/GunsAkimbo/aa6ac81bd55dd1802637

It's not pretty, just a proof-of-concept of the changes to make in order for not be stopped by Incapsula.

GunsAkimbo avatar Nov 28 '15 21:11 GunsAkimbo

So in principle we are interested in making changes like these. However:

  • We are trying to minimize the number of patches we carry relative to upstream Qt. Please discuss that part of your changes with the Qt developers. They'll probably be more receptive to a patch that allows an application to set the ordering itself, than a patch that hardwires an order.
  • Nothing stops Incapsula or whoever from looking for __phantom as well as, or instead of, phantom. The right change there would be to enable the controller script to remove the phantom intrusion entirely, if it isn't needed, and/or rename it as it sees fit.

zackw avatar Nov 28 '15 22:11 zackw

  1. Nope, rejected with reason "Out of scope": https://bugreports.qt.io/browse/QTBUG-49659
  2. I know, it was not meant for a real patch, just something quick and dirty if someone really wanted to compile something that "works".

GunsAkimbo avatar Nov 28 '15 22:11 GunsAkimbo

I'm using PhantomJS for a website screenshotting service, and I'm unable do much on Incapsula protected sites. This is a clear issue for multiple people, don't fully understand why its not being addressed....

yegors avatar Mar 05 '16 00:03 yegors

@yegors Well, PJS uses an external library for network communication (QT), and I think the problem lies partly there, which means we cannot fix that in this repo. The other part is to hide the global object with a fixed name "_phantom" or have the ability to rename it runtime before loading a page.

You could try to compile a build yourself, with the changes mentioned in the gist I linked to a few posts back. Those changes made it possible to pass through the protection, but I'm not sure if both changes were necessary.

As an alternative, you could look into https://phantomjscloud.com/site/index.html I have had good results using this service for incapsula-protected sites.

GunsAkimbo avatar Mar 08 '16 08:03 GunsAkimbo

@GunsAkimbo Its pretty unfortunate that QT refuses to fix it on their end. Custom compile seems like its the best (only) option at this point, as a 3rd party service is out of the question for our applications. Will try your patch and see what happens.

yegors avatar Mar 08 '16 14:03 yegors

Whew, thanks guys for documenting this! I would have wasted hours trying to support incapsula. Will move my scripts now to firefox/selenium.

maximilianh avatar Jul 05 '16 22:07 maximilianh

@GunsAkimbo trying to implement your fix. qhttpnetworkrequest.cpp where is this file located? cant find in this repo.

wowhops avatar Sep 08 '16 08:09 wowhops

@opahopa That file belongs to the QT-repo, it used to be referenced in the .gitmodules-file, I guess that has been changed, according to the history.

GunsAkimbo avatar Sep 08 '16 16:09 GunsAkimbo

@GunsAkimbo any idea how to change the headers order now?

wowhops avatar Sep 09 '16 08:09 wowhops

Since this problem marked as out-of-scope by Qt I believe we can close it too. Because future versions of PhantomJS will use the system-installed (or original version) of Qt.

Also, RFC describes that the order of HTTP headers doesn't matter.

Thanks!

vitallium avatar Sep 09 '16 15:09 vitallium

The order does actually matter. Some anti-bot systems use it to identify phantom and block requests with a particular order.

On Sep 9, 2016 8:02 AM, "Vitaly Slobodin" [email protected] wrote:

Since this problem marked as out-of-scope by Qt I believe we can close it too. Because future versions of PhantomJS will use the system-installed (or original version) of Qt.

Also, RFC describes that the order of HTTP headers doesn't matter.

Thanks!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ariya/phantomjs/issues/11859#issuecomment-245939504, or mute the thread https://github.com/notifications/unsubscribe-auth/AAS-TfrLzR6eBpwK94nJ9a3fE3af2Punks5qoXTsgaJpZM4BWy1A .

maximilianh avatar Sep 09 '16 15:09 maximilianh

Yes, I know that. But the problem is that implementing this feature require a custom version of Qt. We want to move away from custom (patched) version to the original version.

vitallium avatar Sep 09 '16 15:09 vitallium