1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
//
// rhimportd
//
// The Radio Helsinki Rivendell Import Daemon
//
//
// Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhimportd.
//
// rhimportd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// rhimportd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rhimportd. If not, see <http://www.gnu.org/licenses/>.
//
package rhimport
import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/andelf/go-curl"
)
type FetcherCurlCBData struct {
ctx *Context
res *Result
basepath string
filename string
remotename string
metadata map[string]string
conv FetchConverter
totalSize float64
written uint64
writeError error
}
func curlHeaderCallback(ptr []byte, userdata interface{}) bool {
hdr := fmt.Sprintf("%s", ptr)
data := userdata.(*FetcherCurlCBData)
if strings.HasPrefix(hdr, "Content-Disposition:") {
if mediatype, params, err := mime.ParseMediaType(strings.TrimPrefix(hdr, "Content-Disposition:")); err == nil {
if mediatype == "attachment" {
data.filename = filepath.Join(data.basepath, path.Clean("/"+params["filename"]))
}
}
}
return true
}
func curlWriteCallback(ptr []byte, userdata interface{}) bool {
data := userdata.(*FetcherCurlCBData)
if data.conv == nil {
if data.filename == "" {
name := path.Clean("/" + data.remotename)
if name == "/" {
rhdl.Printf("remotename('%s') is invalid, replacing it with 'unnamed'", data.remotename)
name = "unnamed"
}
data.filename = filepath.Join(data.basepath, name)
}
data.ctx.OrigFilename = data.filename
conv, newFilename, err := NewFetchConverter(data.ctx.FetchConverter, data.filename, data.metadata)
if err != nil {
rhl.Printf("Unable to create converter for file %s: %s", data.filename, err)
data.writeError = err
return false
}
data.filename = newFilename
data.conv = conv
}
w, err := data.conv.Write(ptr)
if err != nil {
rhl.Printf("Unable to write to converter(%s): %s", data.filename, err)
data.writeError = err
return false
}
data.written += uint64(w)
if data.written > FILESIZE_MAX {
data.writeError = fmt.Errorf("file exceeds maximum file size")
return false
}
if data.ctx.ProgressCallBack != nil {
if keep := data.ctx.ProgressCallBack(1, "downloading", float64(data.written), data.totalSize, data.ctx.ProgressCallBackData); !keep {
data.ctx.ProgressCallBack = nil
}
}
return true
}
func curlProgressCallback(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
data := userdata.(*FetcherCurlCBData)
if data.writeError != nil {
return false
}
if data.ctx.Cancel != nil && len(data.ctx.Cancel) > 0 {
data.res.ResponseCode = http.StatusNoContent
data.res.ErrorString = "canceled"
return false
}
if dltotal > float64(FILESIZE_MAX) {
data.res.ResponseCode = http.StatusRequestEntityTooLarge
data.res.ErrorString = "file exceeds maximum file size"
return false
}
data.totalSize = dltotal
return true
}
func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) {
rhl.Printf("curl-based fetcher called for '%s'", ctx.SourceUri)
easy := curl.EasyInit()
if easy == nil {
err = fmt.Errorf("Error initializing libcurl")
return
}
defer easy.Cleanup()
easy.Setopt(curl.OPT_FOLLOWLOCATION, true)
easy.Setopt(curl.OPT_URL, ctx.SourceUri)
easy.Setopt(curl.OPT_USERAGENT, "Radio Helsinki Import")
cbdata := &FetcherCurlCBData{ctx: ctx, res: res, remotename: path.Base(uri.Path)}
if cbdata.basepath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil {
return
}
easy.Setopt(curl.OPT_HEADERFUNCTION, curlHeaderCallback)
easy.Setopt(curl.OPT_HEADERDATA, cbdata)
easy.Setopt(curl.OPT_WRITEFUNCTION, curlWriteCallback)
easy.Setopt(curl.OPT_WRITEDATA, cbdata)
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, curlProgressCallback)
easy.Setopt(curl.OPT_PROGRESSDATA, cbdata)
err = easy.Perform()
statusCode := 0
if curlResponseCode, err := easy.Getinfo(curl.INFO_RESPONSE_CODE); err == nil {
if code, ok := curlResponseCode.(int); ok {
statusCode = code
}
}
var convOut string
var convErr error
if cbdata.conv != nil {
cbdata.conv.Close()
rhl.Printf("waiting for converter to finish...")
convOut, convErr, ctx.LoudnessCorr = cbdata.conv.GetResult()
}
if err != nil || cbdata.writeError != nil || convErr != nil {
if cbdata.conv != nil {
rhdl.Printf("Removing stale file: %s", cbdata.filename)
os.Remove(cbdata.filename)
os.Remove(path.Dir(cbdata.filename))
}
if res.ResponseCode == http.StatusNoContent {
rhl.Printf("download of '%s' got canceled", ctx.SourceUri)
return nil
}
if statusCode > 0 && statusCode != http.StatusOK {
res.ResponseCode = statusCode
res.ErrorString = fmt.Sprintf("non-OK response code (%d) while fetching the resource", statusCode)
return nil
}
if cbdata.writeError != nil {
err = cbdata.writeError
}
if convErr != nil {
err = fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut)
rhl.Printf("%v", err)
}
err = fmt.Errorf("curl-fetcher('%s'): %s", ctx.SourceUri, err)
rhl.Println(err)
return
}
rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr)
ctx.SourceFile = cbdata.filename
if ctx.SourceFilePolicy == Auto {
ctx.DeleteSourceFile = true
ctx.DeleteSourceDir = true
}
return
}
var months = [...]string{
"Jänner",
"Februar",
"März",
"April",
"Mai",
"Juni",
"Juli",
"August",
"September",
"Oktober",
"November",
"Dezember",
}
var weekdays = [...]string{
"Sonntag",
"Montag",
"Dienstag",
"Mittwoch",
"Donnerstag",
"Freitag",
"Samstag",
}
func generateArchivFilePath(uri *url.URL) (file, path string, t time.Time, err error) {
if t, err = time.Parse("2006/01/02/15/04", fmt.Sprintf("%s%s", uri.Host, uri.Path)); err != nil {
return
}
file = t.Format("2006-01-02-1504") + ".ogg"
path = fmt.Sprintf("%s/%04d/%02d-%s/%02d-%s", ARCHIV_BASE_PATH, t.Year(), t.Month(), months[t.Month()-1], t.Day(), weekdays[t.Weekday()])
return
}
func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) {
rhdl.Printf("archiv fetcher called for '%s'", ctx.SourceUri)
var srcfile, srcpath string
var start time.Time
if srcfile, srcpath, start, err = generateArchivFilePath(uri); err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("date/time is invalid: %s", err)
return nil
}
end := start.Add(time.Hour)
easy := curl.EasyInit()
if easy == nil {
err = fmt.Errorf("Error initializing libcurl")
return
}
defer easy.Cleanup()
scpuri := fmt.Sprintf("sftp://%s%s/%s", ARCHIV_HOST, srcpath, srcfile)
easy.Setopt(curl.OPT_URL, scpuri)
easy.Setopt(curl.OPT_USERNAME, ARCHIV_USER)
u, _ := user.Current()
easy.Setopt(curl.OPT_SSH_PUBLIC_KEYFILE, fmt.Sprintf("%s/.ssh/id_rsa.pub", u.HomeDir))
easy.Setopt(curl.OPT_SSH_PRIVATE_KEYFILE, fmt.Sprintf("%s/.ssh/id_rsa", u.HomeDir))
var destpath string
if destpath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil {
return
}
cbdata := &FetcherCurlCBData{ctx: ctx, res: res}
cbdata.filename = fmt.Sprintf("%s/%s", destpath, srcfile)
easy.Setopt(curl.OPT_WRITEFUNCTION, curlWriteCallback)
easy.Setopt(curl.OPT_WRITEDATA, cbdata)
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, curlProgressCallback)
easy.Setopt(curl.OPT_PROGRESSDATA, cbdata)
easy.Setopt(curl.OPT_BUFFERSIZE, 1024*1024)
cbdata.metadata = make(map[string]string)
cbdata.metadata["TITLE"] = fmt.Sprintf("Archiv vom %s - %s Uhr", start.Format("2.1.2006 15:04"), end.Format("15:04"))
cbdata.metadata["ALBUM"] = "Radio Helsinki Archiv"
cbdata.metadata["ORGANIZATION"] = "Radio Helsinki"
cbdata.metadata["DATE"] = start.Format("2.1.2006")
rhdl.Printf("importing archiv file from %s", scpuri)
err = easy.Perform()
var convOut string
var convErr error
if cbdata.conv != nil {
cbdata.conv.Close()
rhl.Printf("waiting for converter to finish...")
convOut, convErr, ctx.LoudnessCorr = cbdata.conv.GetResult()
}
if err != nil || cbdata.writeError != nil || convErr != nil {
if cbdata.conv != nil {
rhdl.Printf("Removing stale file: %s", cbdata.filename)
os.Remove(cbdata.filename)
os.Remove(path.Dir(cbdata.filename))
}
if res.ResponseCode == http.StatusNoContent {
rhl.Printf("download of '%s' got canceled", ctx.SourceUri)
return nil
}
if cbdata.writeError != nil {
err = cbdata.writeError
}
if convErr != nil {
err = fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut)
rhl.Printf("%v", err)
}
err = fmt.Errorf("archiv-fetcher('%s'): %s", ctx.SourceUri, err)
rhl.Println(err)
return
}
rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr)
ctx.SourceFile = cbdata.filename
if ctx.SourceFilePolicy == Auto {
ctx.DeleteSourceFile = true
ctx.DeleteSourceDir = true
}
return
}
func fetchFileLocal(ctx *Context, res *Result, uri *url.URL) (err error) {
return fetchFileDir(ctx, res, uri, ctx.conf.LocalFetchDir, true)
}
func fetchFileTmp(ctx *Context, res *Result, uri *url.URL) (err error) {
return fetchFileDir(ctx, res, uri, ctx.conf.TempDir, false)
}
func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal int64) (err error) {
basepath, err := ioutil.TempDir(ctx.conf.TempDir, "rhimportd-")
if err != nil {
return err
}
origDir, origFile := path.Split(ctx.SourceFile)
ctx.OrigFilename = ctx.SourceFile
var conv FetchConverter
if conv, ctx.SourceFile, err = NewFetchConverter(ctx.FetchConverter, filepath.Join(basepath, origFile), nil); err != nil {
rhl.Printf("Unable to create converter for file %s: %s", origDir+origFile, err)
return
}
var buffer [1 * 1024 * 1024]byte
written := uint64(0)
for {
var r, w int
r, err = origSrc.Read(buffer[:])
if err == io.EOF {
err = nil
break
}
if err != nil {
rhl.Printf("Unable to read from source file %s: %s", origDir+origFile, err)
break
}
w, err = conv.Write(buffer[0:r])
if err != nil {
rhl.Printf("Unable to write to converter(%s): %s", ctx.SourceFile, err)
break
}
written += uint64(w)
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "fetching", float64(written), float64(sizeTotal), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
}
conv.Close()
rhl.Printf("waiting for converter to finish...")
var convOut string
var convErr error
if convOut, convErr, ctx.LoudnessCorr = conv.GetResult(); convErr != nil {
if convOut != "" {
rhl.Printf("converter error: %v; converter output: %s", convErr, convOut)
}
return fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut)
}
rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr)
if err != nil {
return err
}
switch ctx.SourceFilePolicy {
case Auto:
ctx.DeleteSourceFile = true
ctx.DeleteSourceDir = true
case Keep:
ctx.DeleteSourceFile = false
ctx.DeleteSourceDir = false
case Delete:
ctx.DeleteSourceDir = true
ctx.DeleteSourceFile = true
os.Remove(origDir + origFile)
case DeleteWithDir:
ctx.DeleteSourceDir = true
ctx.DeleteSourceFile = true
os.Remove(origDir + origFile)
os.Remove(origDir)
}
return
}
func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert bool) (err error) {
rhl.Printf("Dir fetcher called for '%s'", ctx.SourceUri)
ctx.SourceFile = filepath.Join(dir, path.Clean("/"+uri.Path))
var src *os.File
if src, err = os.Open(ctx.SourceFile); err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("local-file open(): %s", err)
return nil
}
defer src.Close()
size := int64(0)
if info, err := src.Stat(); err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("local-file stat(): %s", err)
return nil
} else {
size = info.Size()
if info.IsDir() {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("'%s' is a directory", ctx.SourceFile)
return nil
}
}
if size > FILESIZE_MAX {
res.ResponseCode = http.StatusRequestEntityTooLarge
res.ErrorString = "file exceeds maximum file size"
return nil
}
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "fetching", 0.0, float64(size), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
if convert {
if err = fetchFileDirConvert(ctx, res, src, size); err != nil {
return
}
} else {
if ctx.SourceFilePolicy == Auto {
ctx.DeleteSourceFile = false
ctx.DeleteSourceDir = false
}
}
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "fetching", float64(size), float64(size), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
return
}
func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error {
rhdl.Printf("Fake fetcher for '%s'", ctx.SourceUri)
duration, err := strconv.ParseUint(uri.Host, 10, 32)
if err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = "invalid duration (must be a positive integer)"
return nil
}
for i := uint(0); i < uint(duration); i++ {
if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
rhl.Printf("faking got canceled")
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return nil
}
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "faking", float64(i), float64(duration), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
time.Sleep(100 * time.Millisecond)
}
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "faking", float64(duration), float64(duration), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
ctx.SourceFile = "/nonexistend/fake.mp3"
ctx.OrigFilename = ctx.SourceFile
if ctx.SourceFilePolicy == Auto {
ctx.DeleteSourceFile = false
ctx.DeleteSourceDir = false
}
return nil
}
func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv FetchConverter) error {
cancel := ctx.Cancel
if cancel == nil {
cancel = make(<-chan bool)
}
written := uint64(0)
for {
select {
case <-cancel:
rhl.Printf("receiving attachment '%s' got canceled", ctx.SourceFile)
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return nil
case data, ok := <-ctx.AttachmentChan:
if !ok {
rhl.Printf("receiving attachment '%s' got canceled (channel has been closed)", ctx.SourceFile)
res.ResponseCode = http.StatusNoContent
res.ErrorString = "canceled"
return nil
}
left := sizeTotal - written
if int(left) < len(data) {
rhl.Printf("attachment fetcher: truncating %d byes of extra data", len(data)-int(left))
data = data[0:left]
}
w, err := conv.Write(data)
if err != nil {
rhl.Printf("Unable to write to converter(%s): %s", ctx.SourceFile, err)
return err
}
written += uint64(w)
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "receiving", float64(written), float64(sizeTotal), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
if uint64(written) >= sizeTotal {
return nil
}
}
}
}
func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error {
rhdl.Printf("Attachment fetcher for '%s'", ctx.SourceUri)
if ctx.AttachmentChan == nil {
return fmt.Errorf("attachement channel is nil")
}
sizeTotal, err := strconv.ParseUint(uri.Host, 10, 64)
if err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = "invalid attachment size (must be a positive integer)"
return nil
}
if sizeTotal > FILESIZE_MAX {
res.ResponseCode = http.StatusRequestEntityTooLarge
res.ErrorString = "file exceeds maximum file size"
return nil
}
basepath, err := ioutil.TempDir(ctx.conf.TempDir, "rhimportd-")
if err != nil {
return err
}
ctx.SourceFile = filepath.Join(basepath, path.Clean("/"+uri.Path))
var conv FetchConverter
ctx.OrigFilename = ctx.SourceFile
if conv, ctx.SourceFile, err = NewFetchConverter(ctx.FetchConverter, ctx.SourceFile, nil); err != nil {
rhl.Printf("Unable to create converter for file %s: %s", ctx.OrigFilename, err)
return err
}
if ctx.ProgressCallBack != nil {
if keep := ctx.ProgressCallBack(1, "receiving", 0.0, float64(sizeTotal), ctx.ProgressCallBackData); !keep {
ctx.ProgressCallBack = nil
}
}
err = writeAttachmentFile(ctx, res, sizeTotal, conv)
conv.Close()
rhl.Printf("waiting for converter to finish...")
convOut, convErr, loudnessCorr := conv.GetResult()
if err != nil {
return err
}
if convErr != nil {
if convOut != "" {
rhl.Printf("converter error: %v; converter output: %s", convErr, convOut)
}
return fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut)
}
ctx.LoudnessCorr = loudnessCorr
rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr)
if ctx.SourceFilePolicy == Auto {
ctx.DeleteSourceFile = true
ctx.DeleteSourceDir = true
}
return nil
}
type FetchFunc func(*Context, *Result, *url.URL) (err error)
var (
fetchers = map[string]FetchFunc{
"local": fetchFileLocal,
"tmp": fetchFileTmp,
"fake": fetchFileFake,
"attachment": fetchFileAttachment,
}
curlProtos = map[string]bool{
"http": false, "https": false,
"ftp": false, "ftps": false,
}
)
func fetcherInit() {
archiveEnabled := false
info := curl.VersionInfo(curl.VERSION_FIRST)
protos := info.Protocols
for _, proto := range protos {
if proto == "sftp" {
rhdl.Printf("curl: enabling protocol %s", proto)
fetchers["archiv"] = fetchFileArchiv
archiveEnabled = true
} else if _, ok := curlProtos[proto]; ok {
rhdl.Printf("curl: enabling protocol %s", proto)
fetchers[proto] = fetchFileCurl
curlProtos[proto] = true
} else {
rhdl.Printf("curl: ignoring protocol %s", proto)
}
}
for proto, enabled := range curlProtos {
if !enabled {
rhl.Printf("curl: protocol %s is disabled because the installed library version doesn't support it!", proto)
}
}
if !archiveEnabled {
rhl.Printf("archiv: fetcher is disabled because the installed curl library version doesn't support sFTP!")
}
}
func checkPassword(ctx *Context, res *Result) (err error) {
ok := false
if ok, err = ctx.CheckPassword(); err != nil {
return
}
if !ok {
res.ResponseCode = http.StatusUnauthorized
res.ErrorString = "invalid username and/or password"
}
return
}
func FetchFile(ctx *Context) (res *Result, err error) {
res = &Result{ResponseCode: http.StatusOK}
var uri *url.URL
if uri, err = url.Parse(ctx.SourceUri); err != nil {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("parsing uri: %s", err)
return res, nil
}
if !ctx.Trusted {
if err = checkPassword(ctx, res); err != nil || res.ResponseCode != http.StatusOK {
return
}
}
if fetcher, ok := fetchers[uri.Scheme]; ok {
err = fetcher(ctx, res, uri)
} else {
res.ResponseCode = http.StatusBadRequest
res.ErrorString = fmt.Sprintf("No fetcher for uri scheme '%s' found.", uri.Scheme)
return
}
switch ctx.SourceFilePolicy {
case Keep:
ctx.DeleteSourceFile = false
ctx.DeleteSourceDir = false
case DeleteWithDir:
ctx.DeleteSourceDir = true
fallthrough
case Delete:
ctx.DeleteSourceFile = true
}
return
}
|