Skip to content

Commit 7bde01d

Browse files
committed
version 1.0.0
0 parents  commit 7bde01d

20 files changed

+895
-0
lines changed

Diff for: .github/CODE_OF_CONDUCT.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

Diff for: .github/CONTRIBUTING.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributing
2+
3+
## Report an issue
4+
5+
Please follow the guidelines below when creating an issue so that your issue can be more promptly resolved:
6+
* Provide information including: the version of PHP and This package, the type of operating system and Web server;
7+
* Provide the complete error call stack if available;
8+
* Describe the steps for reproducing the issue. It would be even better if you could provide code to reproduce the issue;
9+
10+
Do not report an issue if you are asking how to use some feature. You should use the `README.md`.
11+
12+
Before you report an issue, please search through existing issues to see if your issue is already reported or fixed to make sure you are not reporting a duplicated issue. Also make sure you have the latest version of package and see if the issue still exists.

Diff for: .github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: "https://www.paypal.me/ddrv"

Diff for: .github/ISSUE_TEMPLATE/bug_report.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: 'Bug'
5+
labels: bug
6+
assignees: ddrv
7+
8+
---
9+
10+
### What steps will reproduce the problem?
11+
12+
### What is the expected result?
13+
14+
### What do you get instead?
15+
16+
### Additional info
17+
18+
| Q | A
19+
| ---------------- | ---
20+
| Library version | 1.?
21+
| PHP version | ?
22+
| Operating system | ?

Diff for: .github/ISSUE_TEMPLATE/custom.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ddrv
7+
8+
---

Diff for: .github/ISSUE_TEMPLATE/feature_request.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature
6+
assignees: ddrv
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

Diff for: .github/PULL_REQUEST_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| Q | A
2+
| ------------- | ---
3+
| Bug fix? | yes/no
4+
| New feature? | yes/no
5+
| Tickets | Fix #...

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
phpcs.xml
4+
phpunit.xml

Diff for: LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Ivan Dudarev, https://github.com/ddrv <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Diff for: README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[![Latest Stable Version](https://img.shields.io/packagist/v/webclient/ext-cookie.svg?style=flat-square)](https://packagist.org/packages/webclient/ext-cookie)
2+
[![Total Downloads](https://img.shields.io/packagist/dt/webclient/ext-cookie.svg?style=flat-square)](https://packagist.org/packages/webclient/ext-cookie/stats)
3+
[![License](https://img.shields.io/packagist/l/webclient/ext-cookie.svg?style=flat-square)](https://github.com/phpwebclient/ext-cookie/blob/master/LICENSE)
4+
[![PHP](https://img.shields.io/packagist/php-v/webclient/ext-cookie.svg?style=flat-square)](https://php.net)
5+
6+
# webclient/ext-cookie
7+
8+
Cookie extension for PSR-18 HTTP client.
9+
10+
# Install
11+
12+
Install this package and your favorite [psr-18 implementation](https://packagist.org/providers/psr/http-client-implementation).
13+
14+
```bash
15+
composer require webclient/ext-cookie:^1.0
16+
```
17+
18+
# Using
19+
20+
```php
21+
<?php
22+
23+
use Psr\Http\Client\ClientInterface;
24+
use Psr\Http\Message\RequestInterface;
25+
use Webclient\Extension\Cookie\Client;
26+
use Webclient\Extension\Cookie\Cookie\Storage;
27+
28+
/**
29+
* @var ClientInterface $client Your PSR-18 HTTP Client
30+
* @var Storage $storage Cookies storage. You may extends this class for implements your storage
31+
*/
32+
$http = new Client($client, $storage);
33+
34+
/** @var RequestInterface $request */
35+
$response = $http->sendRequest($request);
36+
```
37+
38+
# Provided cookies stores
39+
40+
* `\Webclient\Extension\Cookie\Cookie\ArrayStorage` - Cookies stored in memory.
41+
* `\Webclient\Extension\Cookie\Cookie\NetscapeCookieFile` - Cookies stored in a file ([format][https://curl.haxx.se/docs/http-cookies.html]).

Diff for: composer.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "webclient/ext-cookie",
3+
"type": "library",
4+
"description": "Cookies extension for PSR-18 HTTP client",
5+
"keywords": ["http", "client", "http-client", "http-client-extension", "cookie"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Ivan Dudarev",
10+
"email": "[email protected]",
11+
"homepage": "https://ddrv.ru"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0",
16+
"ext-json": "*",
17+
"ext-mbstring": "*",
18+
"psr/http-client": "^1.0"
19+
},
20+
"require-dev": {
21+
"nyholm/psr7": "^1.3",
22+
"phpunit/phpunit": "^6.5",
23+
"squizlabs/php_codesniffer": "^3.5",
24+
"webclient/fake-http-client": "^1.0"
25+
},
26+
"provide": {
27+
"psr/http-client-implementation": "1.0"
28+
},
29+
"suggest": {
30+
"psr/http-client-implementation": "Choice your favorite psr-18 implementation"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"Webclient\\Extension\\Cookie\\": "src/"
35+
}
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"Stuff\\Webclient\\Extension\\Cookie\\": "stuff/",
40+
"Tests\\Webclient\\Extension\\Cookie\\": "tests/"
41+
}
42+
}
43+
}

Diff for: phpcs.xml.dist

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Coding standard">
3+
<description>Coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR12">
12+
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
13+
</rule>
14+
15+
<!-- Paths to check -->
16+
<file>src</file>
17+
<file>stuff</file>
18+
<file>tests</file>
19+
</ruleset>

Diff for: phpunit.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="all">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

Diff for: src/Client.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Extension\Cookie;
6+
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Webclient\Extension\Cookie\Cookie\Storage;
11+
12+
use function array_replace;
13+
use function explode;
14+
use function strtolower;
15+
use function strtotime;
16+
use function trim;
17+
18+
final class Client implements ClientInterface
19+
{
20+
21+
/**
22+
* @var ClientInterface
23+
*/
24+
private $client;
25+
26+
/**
27+
* @var Storage
28+
*/
29+
private $storage;
30+
31+
public function __construct(ClientInterface $client, Storage $storage)
32+
{
33+
$this->client = $client;
34+
$this->storage = $storage;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function sendRequest(RequestInterface $request): ResponseInterface
41+
{
42+
$uri = $request->getUri();
43+
$cookies = $this->storage->get($uri);
44+
if ($cookies) {
45+
if ($request->hasHeader('Cookie')) {
46+
foreach (explode(',', $request->getHeaderLine('Cookie')) as $line) {
47+
$line = trim($line);
48+
list($name, $value) = array_replace([null, null], explode('=', $line, 2));
49+
$name = trim($name);
50+
$value = trim($value);
51+
if (!$name || !$value) {
52+
continue;
53+
}
54+
$cookies[$name] = $value;
55+
}
56+
}
57+
foreach ($cookies as $cookie => $value) {
58+
$request = $request->withAddedHeader('Cookie', $cookie . '=' . $value);
59+
}
60+
}
61+
$response = $this->client->sendRequest($request);
62+
if ($response->hasHeader('Set-Cookie')) {
63+
foreach ($response->getHeader('Set-Cookie') as $header) {
64+
$this->setCookieFromHeader($header, $uri->getHost());
65+
}
66+
}
67+
return $response;
68+
}
69+
70+
public function __destruct()
71+
{
72+
$this->storage->save();
73+
}
74+
75+
/**
76+
* @param string $header
77+
* @param string $domain
78+
*/
79+
private function setCookieFromHeader(string $header, string $domain)
80+
{
81+
$arr = explode(';', $header);
82+
$cookies = [];
83+
$path = '/';
84+
$expires = 0;
85+
$secure = false;
86+
foreach ($arr as $item) {
87+
list($key, $value) = array_replace(['', ''], explode('=', $item, 2));
88+
$key = trim($key);
89+
$value = trim($value);
90+
switch (strtolower($key)) {
91+
case 'domain':
92+
if ($value) {
93+
$domain = $value;
94+
}
95+
break;
96+
case 'path':
97+
$path = $value ? $value : '/';
98+
break;
99+
case 'secure':
100+
$secure = true;
101+
break;
102+
case 'expires':
103+
$timestamp = strtotime($value);
104+
$expires = $timestamp > 0 ? $timestamp : 0;
105+
break;
106+
case 'httponly':
107+
break;
108+
default:
109+
$cookies[$key] = $value;
110+
}
111+
}
112+
foreach ($cookies as $key => $value) {
113+
$this->storage->set($key, $value, $domain, $path, $expires, $secure);
114+
}
115+
}
116+
}

Diff for: src/Cookie/ArrayStorage.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Extension\Cookie\Cookie;
6+
7+
final class ArrayStorage extends Storage
8+
{
9+
10+
public function save()
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)