-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding the HEAD command #33
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf1f3cb
Add Head command interface
05b5a5b
Resolve merge conflicts
0d8fb34
Correct author
60b2205
Revert vendor autoload for github test environment
970bd2b
Lint the file I touched
842a4de
Refactor onHeadFollows
369465c
Refactor HeadCommand to return the string un-parsed
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NNTP library. | ||
* | ||
* (c) Robin van der Vleuten <robin@webstronauts.co> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rvdv\Nntp\Command; | ||
|
||
use Rvdv\Nntp\Exception\RuntimeException; | ||
use Rvdv\Nntp\Response\MultiLineResponse; | ||
use Rvdv\Nntp\Response\Response; | ||
|
||
/** | ||
* HeadCommand. | ||
* | ||
* @author elabz | ||
*/ | ||
class HeadCommand extends Command implements CommandInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $article; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $article | ||
*/ | ||
public function __construct($article) | ||
{ | ||
$this->article = $article; | ||
|
||
parent::__construct(true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke() | ||
{ | ||
return sprintf('HEAD %s', $this->article); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function onHeadFollows(MultiLineResponse $response) | ||
{ | ||
return array_reduce($response->getLines(), function ($headers, $line) { | ||
preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $matches); | ||
if (!empty($matches)) { | ||
$headers[$matches[1]] = trim($matches[2]); | ||
} | ||
|
||
return $headers; | ||
}); | ||
} | ||
|
||
public function onNoNewsGroupCurrentSelected(Response $response) | ||
{ | ||
throw new RuntimeException('A group must be selected first before getting an article header.', $response->getStatusCode()); | ||
} | ||
|
||
public function onNoSuchArticleNumber(Response $response) | ||
{ | ||
throw new RuntimeException('No article with that number.', $response->getStatusCode()); | ||
} | ||
|
||
public function onNoSuchArticleId(Response $response) | ||
{ | ||
throw new RuntimeException('No article with that message-id.', $response->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we do seem to have a genuine problem with multi-line fields within multi-line responses. news.php.net may be quite a bit busier than most, but they seem to have spliced super-long lines with \n\t ins some cases and \n\s in others.
Have you seen a lot of those spliced super-long lines out there in the wild? Any ideas on how to address that issue (if it indeed needs to be addressed)?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be safer to return the head as a string like the pear nntp class does and leave the name/value pair parsing to a wrapper class as not all users will need the data converted like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough @thebandit , I will refactor. Will leave the parsing of the full header to the application. It will essentially become a twin of the body command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well see what @robinvdvleuten has to say as it's his project (I'm just an end-user like yourself). I was just offering a suggestion as it looks like this project tries to mimic the output of the pear class when possible.