4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HTTP リクエストに対する応答の値を、次の HTTP リクエストで使用する

Posted at

Django の csrfmiddlewaretoken のように、HTTP リクエストに対する応答の値を、それ以降のリクエストで使用したい場合があります

Tsung では dyn_variable 要素を使用して、受信した応答の値を取得して、それ以降のリクエストに設定することが可能です

設定例

下記では、Django の /admin/ に GET でアクセスしてヘッダの csrftoken の値を正規表現で取得して、name に指定した変数 csrfmiddlewaretoken に設定しています

そして、その変数を POST の Body に %%_csrfmiddlewaretoken%% として指定してリクエストを送ります

http.xml
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/opt/erlang/R16B03-1/share/tsung/tsung-1.0.dtd">
<tsung loglevel="notice" version="1.0">
  <clients>
    <client host="localhost" />
  </clients>
  <servers>
    <server host="localhost" port="8000" type="tcp"></server>
  </servers>
  <load>
   <arrivalphase phase="1" duration="1" unit="minute">
     <users interarrival="1" unit="second"></users>
   </arrivalphase>
  </load>
  <options>
    <option type="ts_http" name="user_agent">
      <user_agent probability="100">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36</user_agent>
   </option>
  </options>
  <sessions>
    <session name="http-example" probability="100" type="ts_http">
      <request>
        <dyn_variable name="csrfmiddlewaretoken" re="Set-Cookie:\s+csrftoken=([^;]+)"></dyn_variable>
        <http url="/admin/" method="GET"></http>
      </request>
      <request subst="true">
        <http url="/admin/"
              contents="csrfmiddlewaretoken=%%_csrfmiddlewaretoken%%&amp;username=hexa&amp;password=password&amp;this_is_the_login_form=1&amp;next=%2Fadmin%2F"
              content_type="application/x-www-form-urlencoded" method="POST">
        </http>
      </request>
    </session>
  </sessions>
</tsung>

実際に送られる POST とその応答の例

Hypertext Transfer Protocol
    POST /admin/ HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): POST /admin/ HTTP/1.1\r\n]
            [Message: POST /admin/ HTTP/1.1\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Method: POST
        Request URI: /admin/
        Request Version: HTTP/1.1
    Host: localhost:8000\r\n
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36\r\n
    Cookie:  csrftoken=WjizIzQJVvoZV5ZtDklnukaZEZQJTT90\r\n
    Content-Type: application/x-www-form-urlencoded\r\n
    Content-Length: 126\r\n
        [Content length: 126]
    \r\n
    [Full request URI: http://localhost:8000/admin/]
    [HTTP request 1/1]
    [Response in frame: 366]
Line-based text data: application/x-www-form-urlencoded
    csrfmiddlewaretoken=WjizIzQJVvoZV5ZtDklnukaZEZQJTT90&username=hexa&password=password&this_is_the_login_form=1&next=%2Fadmin%2F

Hypertext Transfer Protocol
    HTTP/1.0 302 FOUND\r\n
        [Expert Info (Chat/Sequence): HTTP/1.0 302 FOUND\r\n]
            [Message: HTTP/1.0 302 FOUND\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Version: HTTP/1.0
        Status Code: 302
        Response Phrase: FOUND
    Date: Sat, 19 Apr 2014 17:11:24 GMT\r\n
    Server: WSGIServer/0.1 Python/2.7.2\r\n
    Expires: Sat, 19 Apr 2014 17:11:24 GMT\r\n
    Vary: Cookie\r\n
    Last-Modified: Sat, 19 Apr 2014 17:11:24 GMT\r\n
    Location: http://localhost:8000/admin/\r\n
    Cache-Control: max-age=0\r\n
    X-Frame-Options: SAMEORIGIN\r\n
    Content-Type: text/html; charset=utf-8\r\n
    Set-Cookie:  csrftoken=WJ7b6S6DstF27pbQQNCGvIi3ufBeol7n; expires=Sat, 18-Apr-2015 17:11:24 GMT; Max-Age=31449600; Path=/\r\n
    Set-Cookie:  sessionid=fht48jtcswsroyauyj83ftufzbwk08in; expires=Sat, 03-May-2014 17:11:24 GMT; httponly; Max-Age=1209600; Path=/\r\n
    \r\n
    [HTTP response 1/1]
    [Time since request: 0.166957000 seconds]
    [Request in frame: 348]

参考

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?