Skip to content

Commit 9f30095

Browse files
author
rubyist
committed
Merge branch 'mgmt-download-from-oid' of https://github.com/Yopai/lfs-test-server
2 parents 61d406c + f1f928c commit 9f30095

5 files changed

Lines changed: 65 additions & 13 deletions

File tree

meta_store.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ func (s *MetaStore) Get(v *RequestVars) (*MetaObject, error) {
5555
if !s.authenticate(v.Authorization) {
5656
return nil, newAuthError()
5757
}
58+
meta, error := s.UnsafeGet(v)
59+
return meta, error;
60+
}
5861

62+
// Get retrieves the Meta information for an object given information in
63+
// RequestVars
64+
// DO NOT CHECK authentication, as it is supposed to have been done before
65+
func (s *MetaStore) UnsafeGet(v *RequestVars) (*MetaObject, error) {
5966
var meta MetaObject
6067

6168
err := s.db.View(func(tx *bolt.Tx) error {
@@ -236,6 +243,12 @@ func (s *MetaStore) authenticate(authorization string) bool {
236243
}
237244
user, password := cs[:i], cs[i+1:]
238245

246+
// check Basic Authentication (admin)
247+
ok := checkBasicAuth(user, password, true)
248+
if ok {
249+
return true
250+
}
251+
239252
value := ""
240253

241254
s.db.View(func(tx *bolt.Tx) error {

mgmt-css.rice-box.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030
// register embeddedBox
3131
embedded.RegisterEmbeddedBox(`mgmt/css`, &embedded.EmbeddedBox{
3232
Name: `mgmt/css`,
33-
Time: time.Unix(1443556486, 0),
33+
Time: time.Unix(1443713979, 0),
3434
Dirs: map[string]*embedded.EmbeddedDir{
3535
"": dir1,
3636
},

mgmt-templates.rice-box.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func init() {
2525
}
2626
file7 := &embedded.EmbeddedFile{
2727
Filename: `config.tmpl`,
28-
FileModTime: time.Unix(1443556474, 0),
28+
FileModTime: time.Unix(1443713964, 0),
2929
Content: string([]byte{0x3c, 0x64, 0x69,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f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x4d, 0x65, 0x74, 0x61, 0x44, 0x42, 0x7d, 0x7d, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x20, 0x20, 0x3c, 0x70, 0x3e, 0x3c, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x3e, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x3a, 0x3c, 0x2f, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x3e, 0x20, 0x7b, 0x7b, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x50, 0x61, 0x74, 0x68, 0x7d, 0x7d, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0xa, 0x3c, 0x64, 0x69, 0x76, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x22, 0x3e, 0xa, 0x20, 0x20, 0x3c, 0x70, 0x3e, 0x54, 0x6f, 0x20, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x65, 0x20, 0x61, 0x20, 0x72, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x75, 0x73, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x4c, 0x46, 0x53, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2c, 0x20, 0x61, 0x64, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x6f, 0x72, 0x79, 0x27, 0x73, 0x20, 0x3c, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x2e, 0x67, 0x69, 0x74, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x3c, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x3a, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x20, 0x20, 0x3c, 0x70, 0x72, 0x65, 0x3e, 0xa, 0x3c, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x5b, 0x6c, 0x66, 0x73, 0x5d, 0xa, 0x20, 0x20, 0x20, 0x20, 0x75, 0x72, 0x6c, 0x20, 0x3d, 0x20, 0x22, 0x7b, 0x7b, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x7d, 0x7d, 0x3a, 0x2f, 0x2f, 0x7b, 0x7b, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x7d, 0x7d, 0x2f, 0x22, 0xa, 0x3c, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0xa, 0x3c, 0x2f, 0x70, 0x72, 0x65, 0x3e, 0xa, 0xa, 0x7b, 0x7b, 0x69, 0x66, 0x20, 0x65, 0x71, 0x20, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x20, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x22, 0x7d, 0x7d, 0xa, 0x3c, 0x70, 0x3e, 0x59, 0x6f, 0x75, 0x72, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x75, 0x73, 0x65, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x2e, 0x20, 0x49, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x27, 0x72, 0x65, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x73, 0x65, 0x6c, 0x66, 0x20, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x61, 0x72, 0x65, 0x20, 0x67, 0x65, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x53, 0x53, 0x4c, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2c, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61, 0x6e, 0x20, 0x61, 0x64, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x3c, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x2e, 0x67, 0x69, 0x74, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x3c, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x3a, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x3c, 0x70, 0x72, 0x65, 0x3e, 0xa, 0x3c, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0x5b, 0x68, 0x74, 0x74, 0x70, 0x5d, 0xa, 0x20, 0x20, 0x20, 0x20, 0x73, 0x73, 0x6c, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x20, 0x3d, 0x20, 0x66, 0x61, 0x6c, 0x73, 0x65, 0xa, 0x3c, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x3e, 0xa, 0x3c, 0x2f, 0x70, 0x72, 0x65, 0x3e, 0xa, 0x7b, 0x7b, 0x65, 0x6e, 0x64, 0x7d, 0x7d, 0xa, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0xa}), //++ TODO: optimize? (double allocation) or does compiler already optimize this?
3030
}
3131
file8 := &embedded.EmbeddedFile{
3232
Filename: `objects.tmpl`,
33-
FileModTime: time.Unix(1439927434, 0),
34-
Content: string([]byte{0x3c, 0x64, 0x69, 0x76, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x22, 0x3e, 0xa, 0x20, 0x20, 0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x68, 0x3e, 0x4f, 0x49, 0x44, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x68, 0x3e, 0x53, 0x69, 0x7a, 0x65, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x7b, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x20, 0x2e, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x7d, 0x7d, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x64, 0x3e, 0x7b, 0x7b, 0x2e, 0x4f, 0x69, 0x64, 0x7d, 0x7d, 0x3c, 0x2f, 0x74, 0x64, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x64, 0x3e, 0x7b, 0x7b, 0x2e, 0x53, 0x69, 0x7a, 0x65, 0x7d, 0x7d, 0x3c, 0x2f, 0x74, 0x64, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x7b, 0x65, 0x6e, 0x64, 0x7d, 0x7d, 0xa, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e, 0xa, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0xa}), //++ TODO: optimize? (double allocation) or does compiler already optimize this?
33+
FileModTime: time.Unix(1443713967, 0),
34+
Content: string([]byte{0x3c, 0x64, 0x69, 0x76, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x22, 0x3e, 0xa, 0x20, 0x20, 0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x68, 0x3e, 0x4f, 0x49, 0x44, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x68, 0x3e, 0x53, 0x69, 0x7a, 0x65, 0x3c, 0x2f, 0x74, 0x68, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x7b, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x20, 0x2e, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x7d, 0x7d, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x64, 0x3e, 0x3c, 0x61, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x3d, 0x22, 0x5f, 0x62, 0x6c, 0x61, 0x6e, 0x6b, 0x22, 0x20, 0x68, 0x72, 0x65, 0x66, 0x3d, 0x22, 0x2f, 0x6d, 0x67, 0x6d, 0x74, 0x2f, 0x72, 0x61, 0x77, 0x2f, 0x7b, 0x7b, 0x2e, 0x4f, 0x69, 0x64, 0x7d, 0x7d, 0x22, 0x3e, 0x7b, 0x7b, 0x2e, 0x4f, 0x69, 0x64, 0x7d, 0x7d, 0x3c, 0x2f, 0x61, 0x3e, 0x3c, 0x2f, 0x74, 0x64, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x64, 0x3e, 0x7b, 0x7b, 0x2e, 0x53, 0x69, 0x7a, 0x65, 0x7d, 0x7d, 0x3c, 0x2f, 0x74, 0x64, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x72, 0x3e, 0xa, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x7b, 0x65, 0x6e, 0x64, 0x7d, 0x7d, 0xa, 0x20, 0x20, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e, 0xa, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0xa}), //++ TODO: optimize? (double allocation) or does compiler already optimize this?
3535
}
3636
file9 := &embedded.EmbeddedFile{
3737
Filename: `users.tmpl`,
@@ -42,7 +42,7 @@ func init() {
4242
// define dirs
4343
dir3 := &embedded.EmbeddedDir{
4444
Filename: ``,
45-
DirModTime: time.Unix(1443556474, 0),
45+
DirModTime: time.Unix(1443713967, 0),
4646
ChildFiles: []*embedded.EmbeddedFile{
4747
file4, // .body.tmpl.swp
4848
file5, // .config.tmpl.swp
@@ -60,7 +60,7 @@ func init() {
6060
// register embeddedBox
6161
embedded.RegisterEmbeddedBox(`mgmt/templates`, &embedded.EmbeddedBox{
6262
Name: `mgmt/templates`,
63-
Time: time.Unix(1443556486, 0),
63+
Time: time.Unix(1443713979, 0),
6464
Dirs: map[string]*embedded.EmbeddedDir{
6565
"": dir3,
6666
},

mgmt.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ type pageData struct {
2121
Config *Configuration
2222
Users []*MetaUser
2323
Objects []*MetaObject
24+
Oid string
2425
}
2526

2627
func (a *App) addMgmt(r *mux.Router) {
2728
r.HandleFunc("/mgmt", basicAuth(a.indexHandler)).Methods("GET")
2829
r.HandleFunc("/mgmt/objects", basicAuth(a.objectsHandler)).Methods("GET")
30+
r.HandleFunc("/mgmt/raw/{oid}", basicAuth(a.objectsRawHandler)).Methods("GET")
2931
r.HandleFunc("/mgmt/users", basicAuth(a.usersHandler)).Methods("GET")
3032
r.HandleFunc("/mgmt/add", basicAuth(a.addUserHandler)).Methods("POST")
3133
r.HandleFunc("/mgmt/del", basicAuth(a.delUserHandler)).Methods("POST")
@@ -49,6 +51,17 @@ func cssHandler(w http.ResponseWriter, r *http.Request) {
4951
f.Close()
5052
}
5153

54+
func checkBasicAuth(user string , pass string, ok bool) (bool) {
55+
if !ok {
56+
return false
57+
}
58+
59+
if user != Config.AdminUser || pass != Config.AdminPass {
60+
return false
61+
}
62+
return true
63+
}
64+
5265
func basicAuth(h http.HandlerFunc) http.HandlerFunc {
5366
return func(w http.ResponseWriter, r *http.Request) {
5467
if Config.AdminUser == "" || Config.AdminPass == "" {
@@ -57,13 +70,9 @@ func basicAuth(h http.HandlerFunc) http.HandlerFunc {
5770
}
5871

5972
user, pass, ok := r.BasicAuth()
60-
if !ok {
61-
w.Header().Set("WWW-Authenticate", "Basic realm=mgmt")
62-
writeStatus(w, r, 401)
63-
return
64-
}
6573

66-
if user != Config.AdminUser || pass != Config.AdminPass {
74+
ret := checkBasicAuth(user, pass, ok);
75+
if !ret {
6776
w.Header().Set("WWW-Authenticate", "Basic realm=mgmt")
6877
writeStatus(w, r, 401)
6978
return
@@ -92,6 +101,36 @@ func (a *App) objectsHandler(w http.ResponseWriter, r *http.Request) {
92101
}
93102
}
94103

104+
func (a *App) objectsRawHandler(w http.ResponseWriter, r *http.Request) {
105+
vars := mux.Vars(r);
106+
rv := &RequestVars{
107+
Oid: vars["oid"],
108+
Authorization: r.Header.Get("Authorization"),
109+
}
110+
111+
meta, err := a.metaStore.UnsafeGet(rv)
112+
if err != nil {
113+
if isAuthError(err) {
114+
requireAuth(w, r)
115+
} else {
116+
writeStatus(w, r, 404)
117+
}
118+
return
119+
}
120+
121+
content, err := a.contentStore.Get(meta)
122+
if err != nil {
123+
writeStatus(w, r, 404)
124+
return
125+
}
126+
127+
w.Header().Set("Content-Type", "application/octet-stream");
128+
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s;", vars["oid"]));
129+
w.Header().Set("Content-Transfer-Encoding", "binary");
130+
w.Header().Set("Content-Length", fmt.Sprintf("%d", meta.Size));
131+
io.Copy(w, content)
132+
}
133+
95134
func (a *App) usersHandler(w http.ResponseWriter, r *http.Request) {
96135
users, err := a.metaStore.Users()
97136
if err != nil {

mgmt/templates/objects.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</tr>
77
{{range .Objects}}
88
<tr>
9-
<td>{{.Oid}}</td>
9+
<td><a target="_blank" href="/mgmt/raw/{{.Oid}}">{{.Oid}}</a></td>
1010
<td>{{.Size}}</td>
1111
</tr>
1212
{{end}}

0 commit comments

Comments
 (0)