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

Add custom headers with websocket request #296

Merged
merged 2 commits into from
Jun 4, 2018

Conversation

xiaotianrandom
Copy link

This is a patch to resolve #116 . It parses custom headers from content of a websocket tag with type connect, and send them with the handshake request. For example, we can send custom cookie using the following config:

<request subst="true">
  <websocket type="connect" path="/path/to/ws">
    Cookie: sessionid=%%_sessionid%%
  </websocket>
</request>

I'm new to Erlang. Could any maintainer give me some feedback? If the design is acceptable, I'd like to complete the document.

@tisba
Copy link
Collaborator

tisba commented Mar 4, 2018

Hey @xiaotianrandom. Nice job! :)

One remark regarding the XML configuration. I would suggest, we keep it similar to HTTP headers:

<http url="/bla" method="GET">
  <www_authenticate userid="Aladdin" passwd="open sesame"/>
  <http_header name="Cache-Control" value="no-cache"/>
  <http_header name="Referer" value="http://www.w3.org/"/>
</http>

So basicially this would translate to:

<request subst="true">
  <websocket type="connect" path="/path/to/ws">
    <http_header name="…" value="…"/>
  </websocket>
</request>

Since the initial handshake is plain HTTP, this would also be better, if things like www_authenticate and a like would be added.

What do you think?

@xiaotianrandom
Copy link
Author

@tisba , thanks for your response.

I agree with you that it's better to use http_header tags instead of raw content, and I actually tried this before. But websocket tags originally contains #PCDATA instead of child tags. To keep it similar to HTTP headers, I changed the dtd as follows:

diff --git a/tsung-1.0.dtd b/tsung-1.0.dtd
index 19ae935..e70b75d 100644
--- a/tsung-1.0.dtd
+++ b/tsung-1.0.dtd
@@ -374,7 +374,7 @@ repeat | if | change_type | foreach | set_option | interaction | abort )*>
     certfile    CDATA   #IMPLIED
     dn          CDATA   #IMPLIED
     >
-<!ELEMENT websocket (#PCDATA) >
+<!ELEMENT websocket (#PCDATA | http_header) >
 <!ATTLIST websocket
     type (connect | message | close) #REQUIRED
     ack  (no_ack | parse) #IMPLIED

And tsung complains:

3215- fatal: {illegal_for_mixed_content,once}
Config Error, aborting ! {fatal,
                             {{illegal_for_mixed_content,once},
                              {file,
                                  "/home/toby/.local/share/tsung/tsung-1.0.dtd"},
                              {line,377},
                              {col,45}}}

Is there an easy way to fix this?

Custom headers can be added with a websocket connect request
using http_header elements, similar to HTTP requests.
@xiaotianrandom xiaotianrandom force-pushed the feature/websocket-header branch from 8aab18f to 85973c1 Compare March 18, 2018 06:34
@xiaotianrandom
Copy link
Author

@tisba I figured out how to accept both PCDATA and children in an element in DTD. Now the patch reads user custom headers from http_header elements as you suggested:

<request subst="true">
  <websocket type="connect" path="/path/to/ws">
    <http_header name="Cookie" value="sessionid=%%_sessionid%%"/>
  </websocket>
</request>

I squashed all commits in this PR into one, and pushed --force.

@tisba
Copy link
Collaborator

tisba commented Mar 19, 2018

I like the configuration part, this is much better!

I currently have very little time to test the changes thoroughly, but maybe @nniclausse can take a look? What do you think about this change?

@nniclausse
Copy link
Contributor

Hi. it seems fine.

@xiaotianrandom
Copy link
Author

Is there anything I can do to make this PR merged?

@tisba
Copy link
Collaborator

tisba commented Jun 1, 2018

I've taken your branch for a little test and it seems to work just fine, thanks!

Could you maybe add a little remark to the documentation that we now can pass HTTP headers when connecting? Maybe under https://github.com/processone/tsung/blob/develop/docs/conf-sessions.rst (http://tsung.erlang-projects.org/user_manual/conf-sessions.html?highlight=websocket#websocket).

@xiaotianrandom
Copy link
Author

I added a paragraph in the doc for this patch. Do I need to add a "New in x.x.x" mark? I use 1.7.1, since the latest version is 1.7.0 now.

@tisba
Copy link
Collaborator

tisba commented Jun 4, 2018

Good question about the documentation of Tsung version :) @nniclausse: How do we want to handle hints for features in future Tsung versions? I'll merge this now as is and I'm happy to change the version remark in a follow up.

As for the code itself: 😎! Thank you for your contribution, @xiaotianrandom!

@tisba tisba merged commit a9792cc into processone:develop Jun 4, 2018
@jesseshieh
Copy link

Hi @tisba and @xiaotianrandom, thanks for this awesome enhancement! I tried to use it though and noticed that it was merged into the develop branch and wasn't included in the latest 1.7.0 release. I was just wondering if that was intended or not. Is there a plan to get this included in the next release? Also, what is the purpose of the develop branch?

@tisba
Copy link
Collaborator

tisba commented Dec 13, 2018

@jesseshieh the 1.7.0 release is quite old already (last year). I started an issue for releasing 1.8.0 but I cannot really find time currently to drive this forward :-/

@jesseshieh
Copy link

Oh I'm so sorry, I misread the year! Sorry about that :)

@pskl
Copy link

pskl commented May 28, 2019

Thanks @xiaotianrandom I hope this gets released soon 🙏

@tisba
Copy link
Collaborator

tisba commented May 28, 2019

I'm guessing that a new "official" release will take some time.

But tsung is actually quite simple to build from source.

@xiaotianrandom xiaotianrandom deleted the feature/websocket-header branch September 15, 2020 09:15
@nniclausse nniclausse added this to the 1.8.0 milestone Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow custom headers for establishing websocket connections
5 participants