Skip to content
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 7 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public function body($article)
return $this->sendCommand(new Command\BodyCommand($article));
}

/**
* {@inheritdoc}
*/
public function head($article)
{
return $this->sendCommand(new Command\HeadCommand($article));
}

/**
* {@inheritdoc}
*/
Expand Down
79 changes: 79 additions & 0 deletions src/Command/HeadCommand.php
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);
Copy link
Contributor Author

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!

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.

Copy link
Contributor Author

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

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.

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());
}
}
1 change: 1 addition & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Response implements ResponseInterface
'GroupSelected' => 211, // rfc 3977
'InformationFollows' => 215, // rfc 2980
'ArticleFollows' => 220, //rfc 3977
'HeadFollows' => 221, //rfc 3977 Section 6.2.2
'BodyFollows' => 222, //rfc 3977
'OverviewInformationFollows' => 224, // rfc 2980
'ArticleReceived' => 240, //rfc 3977
Expand Down