Commit 50a9c554 authored by sleitner's avatar sleitner
Browse files

Issue #3285814: Add headers

parent e1d8d255
Loading
Loading
Loading
Loading
+9 −3
Changes for src/Feeds/Fetcher/Form/HttpFetcherPostFeedForm.php: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -18,10 +18,15 @@ class HttpFetcherPostFeedForm extends HttpFetcherFeedForm {
    parent::buildConfigurationForm($form, $form_state, $feed);

    $form = parent::buildConfigurationForm($form, $form_state, $feed);

    $form['headers'] = [
      '#title' => $this->t('Headers'),
      '#description' => $this->t("List of Header values, one pair per line: KEY: VALUE "),
      '#type' => 'textarea',
      '#default_value' => $feed->getConfigurationFor($this->plugin)['headers'],
    ];
    $form['post_parameter'] = [
      '#title' => $this->t('Post parameters:'),
      '#description' => $this->t("List of Post values pairs, one pair per line: KEY: VALUE"),
      '#title' => $this->t('Post parameters'),
      '#description' => $this->t("List of Post values, one pair per line: KEY: VALUE"),
      '#type' => 'textarea',
      '#default_value' => $feed->getConfigurationFor($this->plugin)['post_parameter'],
    ];
@@ -34,6 +39,7 @@ class HttpFetcherPostFeedForm extends HttpFetcherFeedForm {
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
    parent::submitConfigurationForm($form, $form_state, $feed);
    $feed_config = $feed->getConfigurationFor($this->plugin);
    $feed_config['headers'] = $form_state->getValue('headers');
    $feed_config['post_parameter'] = $form_state->getValue('post_parameter');
    $feed->setConfigurationFor($this->plugin, $feed_config);
  }
+11 −2
Changes for src/Feeds/Fetcher/HttpFetcherPost.php: 11 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ class HttpFetcherPost extends HTTPFetcher {
    $sink = $this->fileSystem->tempnam('temporary://', 'feeds_http_fetcher');
    $sink = $this->fileSystem->realpath($sink);

    $response = $this->post($feed->getSource(), $sink, $this->getCacheKey($feed), $feed->getConfigurationFor($this)['post_parameter']);
    $response = $this->post($feed->getSource(), $sink, $this->getCacheKey($feed), $feed->getConfigurationFor($this)['headers'], $feed->getConfigurationFor($this)['post_parameter']);
    // @todo Handle redirects.
    // @codingStandardsIgnoreStart
    // $feed->setSource($response->getEffectiveUrl());
@@ -69,6 +69,8 @@ class HttpFetcherPost extends HTTPFetcher {
   *   resource, path or a StreamInterface object.
   * @param string $cache_key
   *   (optional) The cache key to find cached headers. Defaults to false.
   * @param string $headers
   *   (optional) The headers or form fields. Defaults to ''.
   * @param string $post_parameter
   *   (optional) The post_parameter or form fields. Defaults to ''.
   *
@@ -80,11 +82,18 @@ class HttpFetcherPost extends HTTPFetcher {
   *
   * @see \GuzzleHttp\RequestOptions
   */
  protected function post($url, $sink, $cache_key = FALSE, $post_parameter = '') {
  protected function post($url, $sink, $cache_key = FALSE, $headers = '', $post_parameter = '') {
    $url = Feed::translateSchemes($url);

    $options = [RequestOptions::SINK => $sink];

    if($headers !== '') {
      foreach(explode("\r\n", $headers) as $row) {
        if(preg_match('/(.*?): (.*)/', $row, $matches)) {
          $options[RequestOptions::HEADERS][$matches[1]] = $matches[2];
        }
      }
    }
    if ($post_parameter !== '') {
      foreach (explode("\r\n", $post_parameter) as $row) {
        if (preg_match('/(.*?): (.*)/', $row, $matches)) {