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

resources: Add responseHeaders option to resources.GetRemote #13297

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

bep
Copy link
Member

@bep bep commented Jan 23, 2025

  • These response headers will be included in .Data.Headers if found.
  • The header name matching is case insensitive.
  • Data.Headers is of type map[string][]string
  • In most cases there will be only one value per header key, but e.g. Set-Cookie commonly has multiple values.

Fixes #12521

* These response headers will be included in `.Data.Headers` if found.
* The header name matching is case insensitive.
* `Data.Headers` is of type `map[string][]string`
* In most cases there will be only one value per header key, but e.g. `Set-Cookie` commonly has multiple values.

Fixes gohugoio#12521
@bep bep force-pushed the feat/getremoteheaders-12521 branch from 7838c24 to b0a2173 Compare January 23, 2025 13:50
@bep bep merged commit 68586c8 into gohugoio:master Jan 23, 2025
6 checks passed
@irkode
Copy link

irkode commented Jan 23, 2025

First of all THX real cool stuff . 👍

build that stuff and get the header fields requested and even more than that :-))

had to sleep a night for that one, sry and maybe wait for the docs , but I was eager to play...

  • Data.Headers is of type map[string][]string
  • In most cases there will be only one value per header key, but e.g. Set-Cookie commonly has multiple values.

That sounds like

  • some are real arrays
  • and other just a one element array holding a string (which then must be parsed by users)

Question: are there rules here, which fields are split and returned as array?

  • guess Set-Cookie will be (by your statement above)
  • Link is not

if that would be the case one could write a generic partial for accessing these fields


Here's that great stuff in action: (PoC not robust)

hugo v0.143.0-DEV-a5637831cdf5eeecd1a93a8e1dd7bd4e0fb07fef windows/amd64 BuildDate=2025-01-23T16:53:38Z

WARN  {
  "first": "https://api.github.com/repositories/11180687/tags?per_page=2\u0026page=1",
  "last": "https://api.github.com/repositories/11180687/tags?per_page=2\u0026page=155",
  "next": "https://api.github.com/repositories/11180687/tags?per_page=2\u0026page=101",
  "prev": "https://api.github.com/repositories/11180687/tags?per_page=2\u0026page=99"
}
WARN  NEXT: https://api.github.com/repositories/11180687/tags?per_page=2&page=101
{{ define "main" }}
   {{ $url := "https://api.github.com/repos/gohugoio/hugo/tags?per_page=2&page=100" }}
   {{ $opts := dict "method" "get" "responseHeaders" (slice "Link") }}
   {{ with try (resources.GetRemote $url $opts) }}
      {{ with .Err }}
         {{ errorf "%s" . }}
      {{ else with .Value }}
         {{ $links := partial "getLinks" . }}
         {{ warnf "%s" (debug.Dump $links) }}
         {{ warnf "NEXT: %s" $links.next }}
      {{ else }}
         {{ warnf "Unable to get remote resource %q" $url }}
      {{ end }}
   {{ end }}
{{ end }}

{{ define "partials/getLinks" }}
   {{ $links := dict }}
   {{ with try (index (index .Data.Headers "Link") 0) }}
      {{ with .Value }}
         {{ range split . "," }}
            {{ with split .  ";" }}
               {{ $link := trim (index . 0) "<> " }}
               {{ $rel :=  index (split (index . 1) `"`) 1 }}
               {{ $links = merge $links (dict $rel $link) }}
            {{ end }}
         {{ end }}
      {{ end }}
   {{ else }}
      {{ warnf "NO LINK HERE %v" . }}
   {{ end }}
   {{ return $links }}
{{ end }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .Data.Headers in GetRemote
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy