Browse Source

add resolved request support

main
yggverse 7 months ago
parent
commit
06238a4ce3
  1. 87
      README.md
  2. 23
      src/Client/Request.php

87
README.md

@ -14,12 +14,39 @@ PHP interface for Gemini protocol queries by TLS socket connection
### Request ### Request
``` ``` php
$request = new \Yggverse\Gemini\Client\Request( $request = new \Yggverse\Gemini\Client\Request(
'gemini://betahowto.duckdns.org:1965/archive' 'gemini://betahowto.duckdns.org:1965/archive'
); );
``` ```
**Resolved request (SNI)**
Optionally, provide resolved IP as the second argument
``` php
$request = new \Yggverse\Gemini\Client\Request(
'gemini://betahowto.duckdns.org:1965/archive' // target URL
'94.140.114.89' // resolved IP, skip to use system-wide resolver
);
```
Alternatively, use `setResolvedHost` method of `Request` object before `getResponse`
#### Request::getResolvedHost
``` php
$request->setResolvedHost(
'94.140.114.89'
)
```
* to resolve address with PHP, take a look on the [net-php](https://github.com/YGGverse/net-php) library!
#### Request::setResolvedHost
Get resolved host back
#### Request::setHost #### Request::setHost
#### Request::getHost #### Request::getHost
#### Request::setPort #### Request::setPort
@ -32,7 +59,7 @@ $request = new \Yggverse\Gemini\Client\Request(
Execute requested URL and return raw response Execute requested URL and return raw response
``` ``` php
var_dump( var_dump(
$request->getResponse() $request->getResponse()
); );
@ -42,7 +69,7 @@ var_dump(
This class provides additional features for the raw response operations This class provides additional features for the raw response operations
``` ``` php
$response = new \Yggverse\Gemini\Client\Response( $response = new \Yggverse\Gemini\Client\Response(
$request->getResponse() $request->getResponse()
); );
@ -55,7 +82,7 @@ $response = new \Yggverse\Gemini\Client\Response(
#### Response::setBody #### Response::setBody
#### Response::getBody #### Response::getBody
``` ``` php
var_dump( var_dump(
$response->getBody() $response->getBody()
); );
@ -69,7 +96,7 @@ Object-oriented API for Gemtext
Basic methods to work with `text/gemini` documents Basic methods to work with `text/gemini` documents
``` ``` php
$body = new \Yggverse\Gemini\Gemtext\Body( $body = new \Yggverse\Gemini\Gemtext\Body(
$response->getBody() // gemtext body from client response or .gmi file $response->getBody() // gemtext body from client response or .gmi file
); );
@ -80,7 +107,7 @@ $body = new \Yggverse\Gemini\Gemtext\Body(
#### Body::getH3 #### Body::getH3
#### Body::getLinks #### Body::getLinks
``` ``` php
var_dump( var_dump(
$body->getLinks() // returns array of inline links $body->getLinks() // returns array of inline links
); );
@ -90,7 +117,7 @@ var_dump(
Find context links by protocol as argument, `gemini` by default Find context links by protocol as argument, `gemini` by default
``` ``` php
var_dump( var_dump(
$body->findLinks('http') // returns array of http links found $body->findLinks('http') // returns array of http links found
); );
@ -100,7 +127,7 @@ var_dump(
Strip gemini tags from Gemini document Strip gemini tags from Gemini document
``` ``` php
var_dump( var_dump(
$body->skipTags() // strip all tags $body->skipTags() // strip all tags
); );
@ -121,7 +148,7 @@ Inline links parser.
Allows to extract address, date with timestamp and alt text from link line given Allows to extract address, date with timestamp and alt text from link line given
``` ``` php
foreach ($body->getLinks() as $line) foreach ($body->getLinks() as $line)
{ {
$link = new \Yggverse\Gemini\Gemtext\Link( $link = new \Yggverse\Gemini\Gemtext\Link(
@ -143,7 +170,7 @@ foreach ($body->getLinks() as $line)
This method also validates time format and returns the unix timestamp as linked argument This method also validates time format and returns the unix timestamp as linked argument
``` ``` php
var_dump( var_dump(
$link->getDate( $link->getDate(
$timestamp // get unix time from this variable $timestamp // get unix time from this variable
@ -171,7 +198,7 @@ Allows to simple deploy new apps or make existing website mirror
Read DokuWiki and convert to Gemini Read DokuWiki and convert to Gemini
``` ``` php
$reader = new \Yggverse\Gemini\Dokuwiki\Reader( $reader = new \Yggverse\Gemini\Dokuwiki\Reader(
// optional regex rule set array // optional regex rule set array
); );
@ -184,7 +211,7 @@ $reader = new \Yggverse\Gemini\Dokuwiki\Reader(
Get or change existing regex rule (or just skip by using build-in set) Get or change existing regex rule (or just skip by using build-in set)
``` ``` php
echo $reader->setRule( echo $reader->setRule(
'/subject/ui', '/subject/ui',
'replacement' 'replacement'
@ -196,7 +223,7 @@ echo $reader->setRule(
#### Reader::getMacros #### Reader::getMacros
#### Reader::setMacros #### Reader::setMacros
``` ``` php
echo $reader->setMacros( echo $reader->setMacros(
'~my-macros-key~', '~my-macros-key~',
'~my-macros-value~', '~my-macros-value~',
@ -211,7 +238,7 @@ As wiki has lot of inline links, to make converted document well-readable, this
If you don't like this implementation, feel free to change it by `Reader::setRule` method! If you don't like this implementation, feel free to change it by `Reader::setRule` method!
``` ``` php
echo $reader->toGemini( echo $reader->toGemini(
file_get_contents( file_get_contents(
'/host/data/pages/index.txt' '/host/data/pages/index.txt'
@ -223,7 +250,7 @@ echo $reader->toGemini(
Get document title Get document title
``` ``` php
$gemini = $reader->toGemini( $gemini = $reader->toGemini(
file_get_contents( file_get_contents(
'/host/data/pages/index.txt' '/host/data/pages/index.txt'
@ -239,7 +266,7 @@ echo $reader->getH1(
Get document links Get document links
``` ``` php
$gemini = $reader->toGemini( $gemini = $reader->toGemini(
file_get_contents( file_get_contents(
'/host/data/pages/index.txt' '/host/data/pages/index.txt'
@ -255,7 +282,7 @@ echo $reader->getLinks(
Provides methods for simple and secure interaction with DokuWiki file storage Provides methods for simple and secure interaction with DokuWiki file storage
``` ``` php
$filesystem = new \Yggverse\Gemini\Dokuwiki\Filesystem( $filesystem = new \Yggverse\Gemini\Dokuwiki\Filesystem(
'/host/data' // storage location '/host/data' // storage location
); );
@ -265,7 +292,7 @@ $filesystem = new \Yggverse\Gemini\Dokuwiki\Filesystem(
Return simple array of all files in storage Return simple array of all files in storage
``` ``` php
var_dump ( var_dump (
$filesystem->getList( $filesystem->getList(
'hello:world' 'hello:world'
@ -277,7 +304,7 @@ var_dump (
Return all files under the storage folder in tree format Return all files under the storage folder in tree format
``` ``` php
var_dump ( var_dump (
$filesystem->getTree( $filesystem->getTree(
'hello:world' 'hello:world'
@ -289,7 +316,7 @@ var_dump (
Return pages under the given data directory Return pages under the given data directory
``` ``` php
var_dump ( var_dump (
$filesystem->getPagePathsByPath( $filesystem->getPagePathsByPath(
// absolute path to target data directory (e.g. Filesystem::getDirectoryPathByUri) // absolute path to target data directory (e.g. Filesystem::getDirectoryPathByUri)
@ -302,7 +329,7 @@ var_dump (
Return absolute path to stored page file Return absolute path to stored page file
``` ``` php
var_dump ( var_dump (
$filesystem->getPagePathByUri( $filesystem->getPagePathByUri(
'hello:world' 'hello:world'
@ -315,7 +342,7 @@ var_dump (
Return page URI in `dokuwiki:format` Return page URI in `dokuwiki:format`
``` ``` php
var_dump ( var_dump (
$filesystem->getPageUriByPath( $filesystem->getPageUriByPath(
'/full/path/to/page.txt' '/full/path/to/page.txt'
@ -327,7 +354,7 @@ var_dump (
Return absolute path to stored media file Return absolute path to stored media file
``` ``` php
var_dump ( var_dump (
$filesystem->getMediaPathByUri( $filesystem->getMediaPathByUri(
'hello:world' 'hello:world'
@ -339,7 +366,7 @@ var_dump (
Return file MIME if path match storage item Return file MIME if path match storage item
``` ``` php
var_dump ( var_dump (
$filesystem->getMimeByPath( $filesystem->getMimeByPath(
'/full/path/to/page.txt' '/full/path/to/page.txt'
@ -351,7 +378,7 @@ var_dump (
Return file content if path match storage item Return file content if path match storage item
``` ``` php
var_dump ( var_dump (
$filesystem->getDataByPath( $filesystem->getDataByPath(
'/full/path/to/page.txt' '/full/path/to/page.txt'
@ -363,7 +390,7 @@ var_dump (
Check path exist and match storage item Check path exist and match storage item
``` ``` php
var_dump ( var_dump (
$filesystem->isPath( $filesystem->isPath(
'/full/path/to/page.txt' '/full/path/to/page.txt'
@ -375,7 +402,7 @@ var_dump (
Useful methods to minify controller codebase Useful methods to minify controller codebase
``` ``` php
$helper = new \Yggverse\Gemini\Dokuwiki\Helper( $helper = new \Yggverse\Gemini\Dokuwiki\Helper(
new \Yggverse\Gemini\Dokuwiki\Filesystem(), new \Yggverse\Gemini\Dokuwiki\Filesystem(),
new \Yggverse\Gemini\Dokuwiki\Reader() new \Yggverse\Gemini\Dokuwiki\Reader()
@ -386,7 +413,7 @@ $helper = new \Yggverse\Gemini\Dokuwiki\Helper(
Return simple array of children section links in Gemini format Return simple array of children section links in Gemini format
``` ``` php
var_dump ( var_dump (
$helper->getChildrenSectionLinksByUri( $helper->getChildrenSectionLinksByUri(
'hello:world' 'hello:world'
@ -398,7 +425,7 @@ var_dump (
Return simple array of children page links in Gemini format Return simple array of children page links in Gemini format
``` ``` php
var_dump ( var_dump (
$helper->getChildrenPageLinksByUri( $helper->getChildrenPageLinksByUri(
'hello:world' 'hello:world'
@ -410,7 +437,7 @@ var_dump (
Return page link (that contain document name) in Gemini format Return page link (that contain document name) in Gemini format
``` ``` php
var_dump ( var_dump (
$helper->getPageLinkByPath( $helper->getPageLinkByPath(
$filesystem->getPagePathByUri( $filesystem->getPagePathByUri(

23
src/Client/Request.php

@ -6,6 +6,8 @@ namespace Yggverse\Gemini\Client;
class Request class Request
{ {
private ?string $_ip = null;
private string $_host; private string $_host;
private int $_port; private int $_port;
private string $_path; private string $_path;
@ -20,7 +22,7 @@ class Request
] ]
]; ];
public function __construct(string $url) public function __construct(string $url, ?string $ip = null)
{ {
if ($host = parse_url($url, PHP_URL_HOST)) if ($host = parse_url($url, PHP_URL_HOST))
{ {
@ -75,6 +77,13 @@ class Request
'' ''
); );
} }
if ($ip && false !== filter_var($ip, FILTER_VALIDATE_IP))
{
$this->setResolvedHost(
$ip
);
}
} }
public function setOptions(array $value): void public function setOptions(array $value): void
@ -127,6 +136,16 @@ class Request
return $this->_query; return $this->_query;
} }
public function setResolvedHost(?string $value): void
{
$this->_ip = $value;
}
public function getResolvedHost(): ?string
{
return $this->_ip;
}
public function getResponse( public function getResponse(
int $timeout = 30, // socket timeout, useful for offline resources int $timeout = 30, // socket timeout, useful for offline resources
?int $limit = null, // content length, null for unlimited ?int $limit = null, // content length, null for unlimited
@ -139,7 +158,7 @@ class Request
$connection = stream_socket_client( $connection = stream_socket_client(
sprintf( sprintf(
'tls://%s:%d', 'tls://%s:%d',
$this->_host, $this->_ip ? $this->_ip : $this->_host,
$this->_port $this->_port
), ),
$code, $code,

Loading…
Cancel
Save