GithubHelp home page GithubHelp logo

Serializing Requests about psr7 HOT 4 CLOSED

guzzle avatar guzzle commented on June 2, 2024
Serializing Requests

from psr7.

Comments (4)

Tobion avatar Tobion commented on June 2, 2024

The problem is the scheme is only part of the TCP connection (via port, ssl) and not really part of the request HTTP representation. What you can do, is to set the request target (withRequestTarget) to the absolute-form. See https://tools.ietf.org/html/rfc7230#section-5.3.2
Then the scheme will not get lost and parse_request should be able to identify the URI scheme.

from psr7.

bpolaszek avatar bpolaszek commented on June 2, 2024

Hello Tobion,

Thanks for your quick answer. I followed your advice but this breaks "unserializing":

/**
 * @param \Psr\Http\Message\RequestInterface $request
 * @return string
 */
function serializePSRRequest(\Psr\Http\Message\RequestInterface $request) {
    if ($request->getUri()->getScheme() === 'https') {
        $request = $request->withRequestTarget('absolute-form');
    }
    return \GuzzleHttp\Psr7\str($request);
}

/**
 * @param string $string
 * @return GuzzleHttp\Psr7\Request
 */
function unserializePSRRequest(string $string) {
    return \GuzzleHttp\Psr7\parse_request($string);
}

$secureRequest      = new \GuzzleHttp\Psr7\Request('GET', 'https://example.org');
$unsecureRequest    = new \GuzzleHttp\Psr7\Request('GET', 'http://example.org');
$secureRequestStr   = serializePSRRequest($secureRequest);
$unsecureRequestStr = serializePSRRequest($unsecureRequest);
var_dump($secureRequestStr === $unsecureRequestStr); // false
var_dump(unserializePSRRequest($unsecureRequestStr)); // returns \GuzzleHttp\Psr7\Request object
var_dump(unserializePSRRequest($secureRequestStr)); // throws \InvalidArgumentException: Invalid request string

What would be the best practice?

Thanks,
Ben

from psr7.

Tobion avatar Tobion commented on June 2, 2024

withRequestTarget('absolute-form')

This is not correct. Absolute-form is just the name. This should do it: $request->withRequestTarget((string) $request->getUri())

from psr7.

bpolaszek avatar bpolaszek commented on June 2, 2024

LMAO - #facepalm ^^

Sorry, my skills in raw HTTP messages aren't that good.
Your solution works, wether or not the original URI has an HTTPS scheme.

Thanks! I owe you a beer.
Ben

from psr7.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.