forked from perkeep/perkeep
/
main.go
1259 lines (1165 loc) · 36.1 KB
/
main.go
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2014 The Perkeep Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The publisher command is a server application to publish items from a
// Perkeep server. See also https://camlistore.org/doc/publishing
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"html"
"html/template"
"io"
"io/fs"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
"perkeep.org/internal/httputil"
"perkeep.org/internal/magic"
"perkeep.org/internal/netutil"
"perkeep.org/internal/osutil"
"perkeep.org/pkg/app"
"perkeep.org/pkg/blob"
"perkeep.org/pkg/blobserver"
"perkeep.org/pkg/blobserver/localdisk"
"perkeep.org/pkg/buildinfo"
"perkeep.org/pkg/cacher"
"perkeep.org/pkg/constants"
"perkeep.org/pkg/publish"
"perkeep.org/pkg/schema"
"perkeep.org/pkg/search"
"perkeep.org/pkg/server"
"perkeep.org/pkg/sorted"
_ "perkeep.org/pkg/sorted/kvfile"
"perkeep.org/pkg/types/camtypes"
"perkeep.org/pkg/webserver"
"go4.org/syncutil"
"golang.org/x/crypto/acme/autocert"
)
var (
flagVersion = flag.Bool("version", false, "show version")
)
var (
logger = log.New(os.Stderr, "PUBLISHER: ", log.LstdFlags)
logf = logger.Printf
)
// config is used to unmarshal the application configuration JSON
// that we get from Perkeep when we request it at $CAMLI_APP_CONFIG_URL.
type config struct {
HTTPSCert string `json:"httpsCert,omitempty"` // Path to the HTTPS certificate file.
HTTPSKey string `json:"httpsKey,omitempty"` // Path to the HTTPS key file.
CertManager bool `json:"certManager,omitempty"` // Use Perkeep's Let's Encrypt cache to get a certificate.
RootName string `json:"camliRoot"` // Publish root name (i.e. value of the camliRoot attribute on the root permanode).
MaxResizeBytes int64 `json:"maxResizeBytes,omitempty"` // See constants.DefaultMaxResizeMem
SourceRoot string `json:"sourceRoot,omitempty"` // Path to the app's resources dir, such as html and css files.
GoTemplate string `json:"goTemplate"` // Go html template to render the publication.
CacheRoot string `json:"cacheRoot,omitempty"` // Root path for the caching blobserver. No caching if empty.
}
// appConfig keeps on trying to fetch the extra config from the app handler. If
// it doesn't succed after an hour has passed, the program exits.
func appConfig() (*config, error) {
configURL := os.Getenv("CAMLI_APP_CONFIG_URL")
if configURL == "" {
return nil, fmt.Errorf("Publisher application needs a CAMLI_APP_CONFIG_URL env var")
}
cl, err := app.Client()
if err != nil {
return nil, fmt.Errorf("could not get a client to fetch extra config: %v", err)
}
conf := &config{}
pause := time.Second
giveupTime := time.Now().Add(time.Hour)
for {
err := cl.GetJSON(context.TODO(), configURL, conf)
if err == nil {
break
}
if time.Now().After(giveupTime) {
logger.Fatalf("Giving up on starting: could not get app config at %v: %v", configURL, err)
}
logger.Printf("could not get app config at %v: %v. Will retry in a while.", configURL, err)
time.Sleep(pause)
pause *= 2
}
return conf, nil
}
// setMasterQuery registers with the app handler a master query that includes
// topNode and all its descendants as the search response.
func (ph *publishHandler) setMasterQuery(topNode blob.Ref) error {
query := &search.SearchQuery{
Sort: search.CreatedDesc,
Limit: -1,
Constraint: &search.Constraint{
Permanode: &search.PermanodeConstraint{
Relation: &search.RelationConstraint{
Relation: "parent",
Any: &search.Constraint{
BlobRefPrefix: topNode.String(),
},
},
},
},
Describe: &search.DescribeRequest{
Depth: 1,
Rules: []*search.DescribeRule{
{Attrs: []string{"camliContent", "camliContentImage", "camliMember", "camliPath:*"}},
},
},
}
data, err := json.Marshal(query)
if err != nil {
return err
}
// TODO(mpl): we should use app.Client instead, but a *client.Client
// Post method doesn't let us get the body.
req, err := http.NewRequest("POST", ph.masterQueryURL, bytes.NewReader(data))
if err != nil {
return err
}
if err := addAuth(req); err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(body) != "OK" {
return fmt.Errorf("error setting master query on app handler: %v", string(body))
}
return nil
}
func (ph *publishHandler) refreshMasterQuery() error {
// TODO(mpl): we should use app.Client instead, but a *client.Client
// Post method doesn't let us get the body.
req, err := http.NewRequest("POST", ph.masterQueryURL+"?refresh=1", nil)
if err != nil {
return err
}
if err := addAuth(req); err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests {
// request suppression. let's not consider it an error.
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(body) != "OK" {
return fmt.Errorf("error refreshing master query: %v", string(body))
}
return nil
}
func addAuth(r *http.Request) error {
am, err := app.Auth()
if err != nil {
return err
}
am.AddAuthHeader(r)
return nil
}
func setupTLS(ws *webserver.Server, conf *config) error {
if conf.HTTPSCert != "" && conf.HTTPSKey != "" {
ws.SetTLS(webserver.TLSSetup{
CertFile: conf.HTTPSCert,
KeyFile: conf.HTTPSKey,
})
return nil
}
if !conf.CertManager {
return nil
}
// As all requests to the publisher are proxied through Perkeep's app
// handler, it makes sense to assume that both Perkeep and the publisher
// are behind the same domain name. Therefore, it follows that
// perkeepd's autocert is the one actually getting a cert (and answering
// the challenge) for the both of them. Plus, if they run on the same host
// (default setup), they can't both listen on 443 to answer the TLS-SNI
// challenge.
// TODO(mpl): however, perkeepd and publisher could be running on
// different hosts, in which case we need to find a way for perkeepd to
// share its autocert cache with publisher. But I think that can wait a
// later CL.
hostname := os.Getenv("CAMLI_API_HOST")
hostname = strings.TrimPrefix(hostname, "https://")
hostname = strings.SplitN(hostname, "/", 2)[0]
hostname = strings.SplitN(hostname, ":", 2)[0]
if !netutil.IsFQDN(hostname) {
return fmt.Errorf("cannot ask Let's Encrypt for a cert because %v is not a fully qualified domain name", hostname)
}
logger.Print("TLS enabled, with Let's Encrypt")
// TODO(mpl): we only want publisher to use the same cache as
// perkeepd, and we don't actually need an autocert.Manager.
// So we could just instantiate an autocert.DirCache, and generate
// from there a *tls.Certificate, but it looks like it would mean
// extracting quite a bit of code from the autocert pkg to do it properly.
// Instead, I've opted for using an autocert.Manager (even though it's
// never going to reply to any challenge), but with NOOP for Put and
// Delete, just to be on the safe side. It's simple enough, but there's
// still a catch: there's no ServerName in the clientHello we get, so we
// reinject one so we can simply use the autocert.Manager.GetCertificate
// method as the way to get the certificate from the cache. Is that OK?
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(hostname),
Cache: roCertCacheDir(osutil.DefaultLetsEncryptCache()),
}
ws.SetTLS(webserver.TLSSetup{
CertManager: func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if clientHello.ServerName == "" {
clientHello.ServerName = hostname
}
return m.GetCertificate(clientHello)
},
})
return nil
}
func main() {
flag.Parse()
if *flagVersion {
fmt.Fprintf(os.Stderr, "publisher version: %s\nGo version: %s (%s/%s)\n",
buildinfo.Summary(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
return
}
logf("Starting publisher version %s; Go %s (%s/%s)", buildinfo.Summary(), runtime.Version(),
runtime.GOOS, runtime.GOARCH)
listenAddr, err := app.ListenAddress()
if err != nil {
logger.Fatalf("Listen address: %v", err)
}
conf, err := appConfig()
if err != nil {
logger.Fatalf("no app config: %v", err)
}
ph := newPublishHandler(conf)
masterQueryURL := os.Getenv("CAMLI_APP_MASTERQUERY_URL")
if masterQueryURL == "" {
logger.Fatalf("Publisher application needs a CAMLI_APP_MASTERQUERY_URL env var")
}
ph.masterQueryURL = masterQueryURL
if err := ph.initRootNode(); err != nil {
logf("%v", err)
} else {
if err := ph.setMasterQuery(ph.rootNode); err != nil {
logf("%v", err)
}
}
ws := webserver.New()
ws.Logger = logger
if err := setupTLS(ws, conf); err != nil {
logger.Fatalf("could not setup TLS: %v", err)
}
ws.Handle("/", ph)
if err := ws.Listen(listenAddr); err != nil {
logger.Fatalf("Listen: %v", err)
}
ws.Serve()
}
func newPublishHandler(conf *config) *publishHandler {
cl, err := app.Client()
if err != nil {
logger.Fatalf("could not get a client for the publish handler %v", err)
}
if conf.RootName == "" {
logger.Fatal("camliRoot not found in the app configuration")
}
maxResizeBytes := conf.MaxResizeBytes
if maxResizeBytes == 0 {
maxResizeBytes = constants.DefaultMaxResizeMem
}
var CSSFiles, JSDeps []string
var staticFiles fs.FS = Files
if conf.SourceRoot != "" {
staticFiles = os.DirFS(conf.SourceRoot)
// TODO(mpl): Can I readdir by listing with "/" on Files, even with DirFallBack?
// Apparently not, but retry later.
dir, err := os.Open(conf.SourceRoot)
if err != nil {
logger.Fatal(err)
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
logger.Fatal(err)
}
for _, v := range names {
if strings.HasSuffix(v, ".css") {
CSSFiles = append(CSSFiles, v)
continue
}
// TODO(mpl): document or fix (use a map?) the ordering
// problem: i.e. jquery.js must be sourced before
// publisher.js. For now, just cheat by sorting the
// slice.
if strings.HasSuffix(v, ".js") {
JSDeps = append(JSDeps, v)
}
}
sort.Strings(JSDeps)
} else {
fis, err := Files.ReadDir(".")
if err != nil {
logger.Fatal(err)
}
for _, v := range fis {
name := v.Name()
if strings.HasSuffix(name, ".css") {
CSSFiles = append(CSSFiles, name)
continue
}
if strings.HasSuffix(name, ".js") {
JSDeps = append(JSDeps, name)
}
}
sort.Strings(JSDeps)
}
// TODO(mpl): add all htmls found in Files to the template if none specified?
if conf.GoTemplate == "" {
logger.Fatal("a go template is required in the app configuration")
}
goTemplate, err := goTemplate(staticFiles, conf.GoTemplate)
if err != nil {
logger.Fatal(err)
}
var cache blobserver.Storage
var thumbMeta *server.ThumbMeta
if conf.CacheRoot != "" {
cache, err = localdisk.New(conf.CacheRoot)
if err != nil {
logger.Fatalf("Could not create localdisk cache: %v", err)
}
thumbsCacheDir := filepath.Join(os.TempDir(), "camli-publisher-cache")
if err := os.MkdirAll(thumbsCacheDir, 0700); err != nil {
logger.Fatalf("Could not create cache dir %s for %v publisher: %v", thumbsCacheDir, conf.RootName, err)
}
kv, err := sorted.NewKeyValue(map[string]interface{}{
"type": "kv",
"file": filepath.Join(thumbsCacheDir, conf.RootName+"-thumbnails.kv"),
})
if err != nil {
logger.Fatalf("Could not create kv for %v's thumbs cache: %v", conf.RootName, err)
}
thumbMeta = server.NewThumbMeta(kv)
}
return &publishHandler{
rootName: conf.RootName,
cl: cl,
resizeSem: syncutil.NewSem(maxResizeBytes),
staticFiles: staticFiles,
goTemplate: goTemplate,
CSSFiles: CSSFiles,
JSDeps: JSDeps,
describedCache: make(map[string]*search.DescribedBlob),
cache: cache,
thumbMeta: thumbMeta,
}
}
func goTemplate(files fs.FS, templateFile string) (*template.Template, error) {
f, err := files.Open(templateFile)
if err != nil {
return nil, fmt.Errorf("Could not open template %v: %v", templateFile, err)
}
defer f.Close()
templateBytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("Could not read template %v: %v", templateFile, err)
}
return template.Must(template.New("subject").Parse(string(templateBytes))), nil
}
// We're using this interface in a publishHandler, instead of directly
// a *client.Client, so we can use a fake client in tests.
type client interface {
search.QueryDescriber
GetJSON(ctx context.Context, url string, data interface{}) error
Post(ctx context.Context, url string, bodyType string, body io.Reader) error
blob.Fetcher
}
type publishHandler struct {
rootName string // Publish root name (i.e. value of the camliRoot attribute on the root permanode).
rootNodeMu sync.Mutex
rootNode blob.Ref // Root permanode, origin of all camliPaths for this publish handler.
masterQueryURL string // not guarded by mutex below, because set on startup.
masterQueryMu sync.Mutex
masterQueryDone bool // master query has been registered with the app handler
cl client // Used for searching, and remote storage.
staticFiles fs.FS // For static resources.
goTemplate *template.Template // For publishing/rendering.
CSSFiles []string
JSDeps []string
resizeSem *syncutil.Sem // Limit peak RAM used by concurrent image thumbnail calls.
describedCacheMu sync.RWMutex
describedCache map[string]*search.DescribedBlob // So that each item in a gallery does not actually require a describe round-trip.
cache blobserver.Storage // For caching images and files, or nil.
thumbMeta *server.ThumbMeta // For keeping track of cached images, or nil.
}
func (ph *publishHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ph.rootNodeMu.Lock()
if !ph.rootNode.Valid() {
// we want to retry doing this every time because the rootNode could have been created
// (by e.g. the owner) since last time.
err := ph.initRootNode()
if err != nil {
httputil.ServeError(w, r, fmt.Errorf("No publish root node: %v", err))
ph.rootNodeMu.Unlock()
return
}
}
ph.rootNodeMu.Unlock()
ph.masterQueryMu.Lock()
if !ph.masterQueryDone {
if err := ph.setMasterQuery(ph.rootNode); err != nil {
httputil.ServeError(w, r, fmt.Errorf("master query not set: %v", err))
ph.masterQueryMu.Unlock()
return
}
ph.masterQueryDone = true
}
ph.masterQueryMu.Unlock()
preq, err := ph.NewRequest(w, r)
if err != nil {
httputil.ServeError(w, r, fmt.Errorf("Could not create publish request: %v", err))
return
}
preq.serveHTTP()
}
func (ph *publishHandler) initRootNode() error {
var getRootNode = func() (blob.Ref, error) {
result, err := ph.camliRootQuery()
if err != nil {
return blob.Ref{}, fmt.Errorf("could not find permanode for root %q of publish handler: %v", ph.rootName, err)
}
if len(result.Blobs) == 0 || !result.Blobs[0].Blob.Valid() {
return blob.Ref{}, fmt.Errorf("could not find permanode for root %q of publish handler: %v", ph.rootName, os.ErrNotExist)
}
return result.Blobs[0].Blob, nil
}
node, err := getRootNode()
if err != nil {
return err
}
ph.rootNode = node
return nil
}
func (ph *publishHandler) camliRootQuery() (*search.SearchResult, error) {
// TODO(mpl): I've voluntarily omitted the owner because it's not clear to
// me that we actually care about that. Same for signer in lookupPathTarget.
return ph.cl.Query(context.TODO(), &search.SearchQuery{
Limit: 1,
Constraint: &search.Constraint{
Permanode: &search.PermanodeConstraint{
Attr: "camliRoot",
Value: ph.rootName,
},
},
})
}
func (ph *publishHandler) lookupPathTarget(root blob.Ref, suffix string) (blob.Ref, error) {
if suffix == "" {
return root, nil
}
// TODO: verify it's optimized: http://perkeep.org/issue/405
result, err := ph.cl.Query(context.TODO(), &search.SearchQuery{
Limit: 1,
Constraint: &search.Constraint{
Permanode: &search.PermanodeConstraint{
SkipHidden: true,
Relation: &search.RelationConstraint{
Relation: "parent",
EdgeType: "camliPath:" + suffix,
Any: &search.Constraint{
BlobRefPrefix: root.String(),
},
},
},
},
})
if err != nil {
return blob.Ref{}, err
}
if len(result.Blobs) == 0 || !result.Blobs[0].Blob.Valid() {
return blob.Ref{}, os.ErrNotExist
}
return result.Blobs[0].Blob, nil
}
// Given a blobref and a few hex characters of the digest of the next hop, return the complete
// blobref of the prefix, if that's a valid next hop.
func (ph *publishHandler) resolvePrefixHop(parent blob.Ref, prefix string) (child blob.Ref, err error) {
// TODO: this is a linear scan right now. this should be
// optimized to use a new database table of members so this is
// a quick lookup. in the meantime it should be in memcached
// at least.
if len(prefix) < 8 {
return blob.Ref{}, fmt.Errorf("Member prefix %q too small", prefix)
}
des, err := ph.describe(parent)
if err != nil {
return blob.Ref{}, fmt.Errorf("Failed to describe member %q in parent %q", prefix, parent)
}
if des.Permanode != nil {
cr, ok := des.ContentRef()
if ok && strings.HasPrefix(cr.Digest(), prefix) {
return cr, nil
}
for _, member := range des.Members() {
if strings.HasPrefix(member.BlobRef.Digest(), prefix) {
return member.BlobRef, nil
}
}
crdes, err := ph.describe(cr)
if err != nil {
return blob.Ref{}, fmt.Errorf("Failed to describe content %q of parent %q", cr, parent)
}
if crdes.Dir != nil {
return ph.resolvePrefixHop(cr, prefix)
}
} else if des.Dir != nil {
for _, child := range des.DirChildren {
if strings.HasPrefix(child.Digest(), prefix) {
return child, nil
}
}
}
return blob.Ref{}, fmt.Errorf("Member prefix %q not found in %q", prefix, parent)
}
func (ph *publishHandler) describe(br blob.Ref) (*search.DescribedBlob, error) {
ph.describedCacheMu.RLock()
if des, ok := ph.describedCache[br.String()]; ok {
ph.describedCacheMu.RUnlock()
return des, nil
}
ph.describedCacheMu.RUnlock()
ctx := context.TODO()
res, err := ph.cl.Describe(ctx, &search.DescribeRequest{
BlobRef: br,
Depth: 1,
})
if err != nil {
return nil, fmt.Errorf("Could not describe %v: %v", br, err)
}
// TODO(mpl): check why Describe is not giving us an error when br is invalid.
if res == nil || res.Meta == nil || res.Meta[br.String()] == nil {
return nil, fmt.Errorf("Could not describe %v", br)
}
return res.Meta[br.String()], nil
}
func (ph *publishHandler) deepDescribe(br blob.Ref) (*search.DescribeResponse, error) {
res, err := ph.cl.Query(context.TODO(), &search.SearchQuery{
Constraint: &search.Constraint{
BlobRefPrefix: br.String(),
CamliType: "permanode",
},
Describe: &search.DescribeRequest{
Depth: 1,
Rules: []*search.DescribeRule{
{
Attrs: []string{"camliContent", "camliContentImage", "camliMember", "camliPath:*"},
},
},
},
Limit: -1,
})
if err != nil {
return nil, fmt.Errorf("Could not deep describe %v: %v", br, err)
}
if res == nil || res.Describe == nil {
return nil, fmt.Errorf("no describe result for %v", br)
}
return res.Describe, nil
}
// publishRequest is the state around a single HTTP request to the
// publish handler
type publishRequest struct {
ph *publishHandler
rw http.ResponseWriter
req *http.Request
base, suffix, subres string
rootpn blob.Ref
subject blob.Ref
inSubjectChain map[string]bool // blobref -> true
subjectBasePath string
publishedRoot blob.Ref // on camliRoot, camliPath:somePath = publishedRoot
}
func (ph *publishHandler) NewRequest(w http.ResponseWriter, r *http.Request) (*publishRequest, error) {
// splits a path request into its suffix and subresource parts.
// e.g. /blog/foo/camli/res/file/xxx -> ("foo", "file/xxx")
base := app.PathPrefix(r)
suffix, res := strings.TrimPrefix(r.URL.Path, base), ""
if strings.HasPrefix(suffix, "-/") {
suffix, res = "", suffix[2:]
} else if s := strings.SplitN(suffix, "/-/", 2); len(s) == 2 {
suffix, res = s[0], s[1]
}
return &publishRequest{
ph: ph,
rw: w,
req: r,
suffix: strings.TrimSuffix(suffix, "/"),
base: base,
subres: res,
rootpn: ph.rootNode,
inSubjectChain: make(map[string]bool),
subjectBasePath: "",
}, nil
}
func (pr *publishRequest) serveHTTP() {
if !pr.rootpn.Valid() {
http.NotFound(pr.rw, pr.req)
return
}
if pr.suffix == "" {
// Do not show everything at the root.
http.Error(pr.rw, "403 forbidden", http.StatusForbidden)
return
}
if pr.Debug() {
pr.rw.Header().Set("Content-Type", "text/html")
pr.pf("I am publish handler at base %q, serving root %q (permanode=%s), suffix %q, subreq %q<hr>",
pr.base, pr.ph.rootName, pr.rootpn, html.EscapeString(pr.suffix), html.EscapeString(pr.subres))
}
if err := pr.findSubject(); err != nil {
if err == os.ErrNotExist {
http.NotFound(pr.rw, pr.req)
return
}
logf("Error looking up %s/%q: %v", pr.rootpn, pr.suffix, err)
pr.rw.WriteHeader(500)
return
}
if pr.Debug() {
pr.pf("<p><b>Subject:</b> <a href='/ui/?p=%s'>%s</a></p>", pr.subject, pr.subject)
return
}
switch pr.subresourceType() {
case "":
// This should not happen too often as now that we have the
// javascript code we're not hitting that code path often anymore
// in a typical navigation. And the app handler is doing
// request suppression anyway.
// If needed, we can always do it in a narrower case later.
if err := pr.ph.refreshMasterQuery(); err != nil {
logf("Error refreshing master query: %v", err)
pr.rw.WriteHeader(500)
return
}
pr.serveSubjectTemplate()
case "b":
// TODO: download a raw blob
case "f": // file download
pr.serveSubresFileDownload()
case "i": // image, scaled
pr.serveSubresImage()
case "s": // static
pr.req.URL.Path = pr.subres[len("/=s"):]
if len(pr.req.URL.Path) <= 1 {
http.Error(pr.rw, "Illegal URL.", http.StatusNotFound)
return
}
file := pr.req.URL.Path[1:]
server.ServeStaticFile(pr.rw, pr.req, pr.ph.staticFiles, file)
case "z":
pr.serveZip()
default:
pr.rw.WriteHeader(400)
pr.pf("<p>Invalid or unsupported resource request.</p>")
}
}
func (pr *publishRequest) Debug() bool {
return pr.req.FormValue("debug") == "1"
}
var memberRE = regexp.MustCompile(`^/?h([0-9a-f]+)`)
func (pr *publishRequest) findSubject() error {
if strings.HasPrefix(pr.suffix, "=s/") {
pr.subres = "/" + pr.suffix
return nil
}
subject, err := pr.ph.lookupPathTarget(pr.rootpn, pr.suffix)
if err != nil {
return err
}
pr.publishedRoot = subject
if strings.HasPrefix(pr.subres, "=z/") {
// this happens when we are at the root of the published path,
// e.g /base/suffix/-/=z/foo.zip
// so we need to reset subres as fullpath so that it is detected
// properly when switching on pr.subresourceType()
pr.subres = "/" + pr.subres
// since we return early, we set the subject because that is
// what is going to be used as a root node by the zip handler.
pr.subject = subject
return nil
}
pr.inSubjectChain[subject.String()] = true
pr.subjectBasePath = pr.base + pr.suffix
// Chase /h<xxxxx> hops in suffix.
for {
m := memberRE.FindStringSubmatch(pr.subres)
if m == nil {
break
}
match, memberPrefix := m[0], m[1]
if err != nil {
return fmt.Errorf("Error looking up potential member %q in describe of subject %q: %v",
memberPrefix, subject, err)
}
subject, err = pr.ph.resolvePrefixHop(subject, memberPrefix)
if err != nil {
return err
}
pr.inSubjectChain[subject.String()] = true
pr.subres = pr.subres[len(match):]
pr.subjectBasePath = addPathComponent(pr.subjectBasePath, match)
}
pr.subject = subject
return nil
}
func (pr *publishRequest) subresourceType() string {
if len(pr.subres) >= 3 && strings.HasPrefix(pr.subres, "/=") {
return pr.subres[2:3]
}
return ""
}
func (pr *publishRequest) pf(format string, args ...interface{}) {
fmt.Fprintf(pr.rw, format, args...)
}
func addPathComponent(base, addition string) string {
if !strings.HasPrefix(addition, "/") {
addition = "/" + addition
}
if strings.Contains(base, "/-/") {
return base + addition
}
return base + "/-" + addition
}
// var hopRE = regexp.MustCompile(fmt.Sprintf("^/%s([0-9a-f]{%d})", digestPrefix, digestLen))
func getFileInfo(item blob.Ref, peers map[string]*search.DescribedBlob) (path []blob.Ref, fi *camtypes.FileInfo, ok bool) {
described := peers[item.String()]
if described == nil ||
described.Permanode == nil ||
described.Permanode.Attr == nil {
return
}
contentRef := described.Permanode.Attr.Get("camliContent")
if contentRef == "" {
return
}
if cdes := peers[contentRef]; cdes != nil && cdes.File != nil {
return []blob.Ref{described.BlobRef, cdes.BlobRef}, cdes.File, true
}
return
}
// serveSubjectTemplate creates the funcs to generate the PageHeader, PageFile,
// and pageMembers that can be used by the subject template, and serves the template.
func (pr *publishRequest) serveSubjectTemplate() {
res, err := pr.ph.deepDescribe(pr.subject)
if err != nil {
httputil.ServeError(pr.rw, pr.req, err)
return
}
pr.ph.cacheDescribed(res.Meta)
subdes := res.Meta[pr.subject.String()]
if subdes.CamliType == schema.TypeFile {
pr.serveFileDownload(subdes)
return
}
headerFunc := func() *publish.PageHeader {
return pr.subjectHeader(res.Meta)
}
fileFunc := func() *publish.PageFile {
file, err := pr.subjectFile(res.Meta)
if err != nil {
logf("%v", err)
return nil
}
return file
}
membersFunc := func() *publish.PageMembers {
members, err := pr.subjectMembers(res.Meta)
if err != nil {
logf("%v", err)
return nil
}
return members
}
page := &publish.SubjectPage{
Header: headerFunc,
File: fileFunc,
Members: membersFunc,
}
err = pr.ph.goTemplate.Execute(pr.rw, page)
if err != nil {
logf("Error serving subject template: %v", err)
http.Error(pr.rw, "Error serving template", http.StatusInternalServerError)
return
}
}
const cacheSize = 1000
func (ph *publishHandler) cacheDescribed(described map[string]*search.DescribedBlob) {
ph.describedCacheMu.Lock()
defer ph.describedCacheMu.Unlock()
if len(ph.describedCache) > cacheSize {
ph.describedCache = described
return
}
for k, v := range described {
ph.describedCache[k] = v
}
}
func (pr *publishRequest) serveFileDownload(des *search.DescribedBlob) {
fileref, fileinfo, ok := pr.fileSchemaRefFromBlob(des)
if !ok {
logf("Didn't get file schema from described blob %q", des.BlobRef)
return
}
mimeType := ""
if fileinfo != nil {
if fileinfo.IsImage() || fileinfo.IsText() {
mimeType = fileinfo.MIMEType
if mimeType == "" {
mimeType = magic.MIMETypeByExtension(filepath.Ext(fileinfo.FileName))
}
}
}
dh := &server.DownloadHandler{
Fetcher: cacher.NewCachingFetcher(pr.ph.cache, pr.ph.cl),
ForceMIME: mimeType,
}
dh.ServeFile(pr.rw, pr.req, fileref)
}
// Given a described blob, optionally follows a camliContent and
// returns the file's schema blobref and its fileinfo (if found).
func (pr *publishRequest) fileSchemaRefFromBlob(des *search.DescribedBlob) (fileref blob.Ref, fileinfo *camtypes.FileInfo, ok bool) {
if des == nil {
http.NotFound(pr.rw, pr.req)
return
}
if des.Permanode != nil {
// TODO: get "forceMime" attr out of the permanode? or
// fileName content-disposition?
if cref := des.Permanode.Attr.Get("camliContent"); cref != "" {
cbr, ok2 := blob.Parse(cref)
if !ok2 {
http.Error(pr.rw, "bogus camliContent", 500)
return
}
des = des.PeerBlob(cbr)
if des == nil {
http.Error(pr.rw, "camliContent not a peer in describe", 500)
return
}
}
}
if des.CamliType == "file" {
return des.BlobRef, des.File, true
}
http.Error(pr.rw, "failed to find fileSchemaRefFromBlob", 404)
return
}
// subjectHeader returns the PageHeader corresponding to the described subject.
func (pr *publishRequest) subjectHeader(described map[string]*search.DescribedBlob) *publish.PageHeader {
subdes := described[pr.subject.String()]
header := &publish.PageHeader{
Title: html.EscapeString(getTitle(subdes.BlobRef, described)),
CSSFiles: pr.cssFiles(),
JSDeps: pr.jsDeps(),
Subject: pr.subject,
Host: pr.req.Host,
SubjectBasePath: pr.subjectBasePath,
PathPrefix: pr.base,
PublishedRoot: pr.publishedRoot,
}
return header
}
func (pr *publishRequest) cssFiles() []string {
files := []string{}
for _, filename := range pr.ph.CSSFiles {
files = append(files, pr.staticPath(filename))
}
return files
}
func (pr *publishRequest) jsDeps() []string {
files := []string{}
for _, filename := range pr.ph.JSDeps {
files = append(files, pr.staticPath(filename))
}
return files
}
func (pr *publishRequest) staticPath(fileName string) string {
return pr.base + "=s/" + fileName
}
func getTitle(item blob.Ref, peers map[string]*search.DescribedBlob) string {