swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
233 stars 59 forks source link

Swoole curl incorrect send cookie from COOKIEFILE/COOKIEJAR. #85

Closed billionaire closed 3 years ago

billionaire commented 3 years ago

Hi. Recently i discovered the php-swoole library and was surprised how easy it is to write high-performance applications with it in the paradigm familiar to php.

But in the module SWOOLE_HOOK_CURL i found that the library does not work correctly with cookie-files.

  1. php-curl (libcurl) use Netscape-format of cookie-file, but SWOOLE_HOOK_CURL doesn't understand this format.
  2. Even if we dont use cookies from php-curl, but let SWOOLE_HOOK_CURL create a cookie from scratch, SWOOLE_HOOK_CURL will not able to reuse cookies (new request will be executed with broken headers).

Working with a cookie is more complex than just taking the content of the Set-Cookie header and saving it to a file, then sending everything in the Cookie-header. The server sends each cookie to the client as a separate Set-Cookie header with all service information (for example, lifetime, domain, etc.), and the client must send only the name and contents of the cookie to the server, checking whether the lifetime has expired, whether the domain is correct and stuff like that. I am sure you know better than me how http-cookies work. Example of how to work correctly with cookie-file can be seen in the library amphp/http-client-cookies.

https://www.php.net/manual/en/function.curl-setopt.php CURLOPT_COOKIEFILE file to read cookies before request. CURLOPT_COOKIEJAR file where cookies are written after curl_close().

Hopefully these bugs will be fixed and php-swoole-curl will handle cookie-files correctly.

Thank you for your work.

sy-records commented 3 years ago

Thank you for your feedback.

Are you using swoole_library on its own or are you using the swoole extension directly?

By the way, in the new swoole version (v4.6.0), we will support the native curl hook. We have released a beta version, this should be compatible and you can test it

What's wrong with this now? Can you provide a reproduction code?

https://curl.se/libcurl/c/CURLOPT_COOKIEFILE.html

The cookie data can be in either the old Netscape / Mozilla cookie data format or just regular HTTP headers (Set-Cookie style) dumped to a file.

billionaire commented 3 years ago

I used php-swoole-extension v4.5.9 installed via pecl install swoole. Now i have installed from source v4.6.0-beta along with --enable-swoole-curl and now everything is perfect when using SWOOLE_HOOK_NATIVE_CURL. You are the best. 👍

root@demo:/usr/src/swoole-src-4.6.0-beta# php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.6.0-beta
Built => Dec 18 2020 10:11:32
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1g  21 Apr 2020
http2 => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608