Skip to content

Commit

Permalink
Merge pull request #18 from NBISweden/cookiehandling
Browse files Browse the repository at this point in the history
Pick up cookies from dataset request and pass later on
  • Loading branch information
pontus authored Jan 15, 2025
2 parents 8b79f3e + 2560d87 commit 0ab2eca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
4 changes: 3 additions & 1 deletion internal/httpreader/httpreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (r *HTTPReader) doFetch(rangeSpec string) ([]byte, error) {

if r.conf.Headers != nil {
for h := range *r.conf.Headers {
req.Header.Add(h, r.conf.Headers.Get(h))
for _, v := range r.conf.Headers.Values(h) {
req.Header.Add(h, v)
}
}
}

Expand Down
60 changes: 43 additions & 17 deletions internal/sdafs/sdafs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ type SDAfs struct {

// Conf holds the configuration
type Conf struct {
CredentialsFile string
RootURL string
RemoveSuffix bool
SpecifyUID bool
SpecifyGID bool
UID uint32
GID uint32
SpecifyDirPerms bool
SpecifyFilePerms bool
SkipLevels int
MaxRetries int
DirPerms os.FileMode
FilePerms os.FileMode
HTTPClient *http.Client
ChunkSize int
CredentialsFile string
RootURL string
RemoveSuffix bool
SpecifyUID bool
SpecifyGID bool
UID uint32
GID uint32
SpecifyDirPerms bool
SpecifyFilePerms bool
SkipLevels int
MaxRetries int
DirPerms os.FileMode
FilePerms os.FileMode
HTTPClient *http.Client
ChunkSize int
SessionCookieName string
}

// inode is the struct to manage a directory entry
Expand Down Expand Up @@ -167,6 +168,30 @@ func (s *SDAfs) doRequest(relPath, method string) (*http.Response, error) {
return s.client.Do(req)
}

// extractCookies picks up cookies from the response and sets them for further
// use
func (s *SDAfs) extractCookies(r *http.Response) {
log.Printf("extracting cookies from %v", r.Header)
setCookies := r.Header.Values("set-cookie")

newCookies := ""

// No cookie instructions
if len(setCookies) == 0 {
return
}

for _, p := range setCookies {
cookie, _, _ := strings.Cut(p, ";")
if newCookies != "" {
newCookies += "; "
}
newCookies = newCookies + cookie
}

s.keyHeader.Set("cookie", newCookies)
}

func (s *SDAfs) getDatasets() error {
r, err := s.doRequest("/metadata/datasets", "GET")

Expand All @@ -183,6 +208,9 @@ func (s *SDAfs) getDatasets() error {
r.StatusCode)
}

// Pick up cookies for further use
s.extractCookies(r)

text, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf(
Expand Down Expand Up @@ -516,8 +544,6 @@ func idToNum(s string) uint32 {

// setup makes various initialisations
func (s *SDAfs) setup() error {
//

if s.conf == nil {
return fmt.Errorf("no configuration specified")
}
Expand Down

0 comments on commit 0ab2eca

Please sign in to comment.