Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cdn-origin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
easrng
cdn-origin
Commits
655fac38
Commit
655fac38
authored
7 years ago
by
Dean
Browse files
Options
Downloads
Patches
Plain Diff
add Range and Accept-Encoding support
parent
eb2915b9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cdn-origin.go
+29
-5
29 additions, 5 deletions
cdn-origin.go
weed/seaweed.go
+25
-12
25 additions, 12 deletions
weed/seaweed.go
with
54 additions
and
17 deletions
cdn-origin.go
+
29
−
5
View file @
655fac38
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"net"
"strconv"
"strings"
"time"
...
...
@@ -237,6 +238,7 @@ func recordMetrics(ctx *fasthttp.RequestCtx, objectType string) {
}
hostBytes
:=
ctx
.
Request
.
Header
.
Peek
(
host
)
statusCode
:=
ctx
.
Response
.
StatusCode
()
if
len
(
hostBytes
)
!=
0
{
go
func
()
{
// Check hostname
...
...
@@ -256,7 +258,7 @@ func recordMetrics(ctx *fasthttp.RequestCtx, objectType string) {
record
.
CountryCode
=
countryCode
record
.
Hostname
=
hostStr
record
.
ObjectType
=
objectType
record
.
StatusCode
=
ctx
.
Response
.
S
tatusCode
()
record
.
StatusCode
=
s
tatusCode
err
=
collector
.
Put
(
record
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Warn
(
"failed to collect record"
)
...
...
@@ -320,8 +322,17 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
return
}
// Construct headers and get object
headers
:=
map
[
string
][]
byte
{}
if
acceptEncoding
:=
ctx
.
Request
.
Header
.
Peek
(
"Accept-Encoding"
);
len
(
acceptEncoding
)
>
0
{
headers
[
"Accept-Encoding"
]
=
acceptEncoding
}
if
reqRange
:=
ctx
.
Request
.
Header
.
Peek
(
"Range"
);
len
(
reqRange
)
>
0
{
headers
[
"Range"
]
=
reqRange
}
fmt
.
Println
(
headers
)
// TODO: ?thumbnail query parameter for images
statusCode
,
contentSize
,
err
:=
seaweed
.
Get
(
ctx
,
backendFileID
.
String
,
defaultSeaweedFSQueryParameters
)
statusCode
,
resHeaders
,
err
:=
seaweed
.
Get
(
ctx
,
backendFileID
.
String
,
headers
,
defaultSeaweedFSQueryParameters
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Warn
(
"failed to retrieve file from SeaweedFS volume server"
)
ctx
.
SetStatusCode
(
fasthttp
.
StatusInternalServerError
)
...
...
@@ -330,9 +341,9 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
recordMetrics
(
ctx
,
metricsObjectType
)
return
}
if
statusCode
!=
fasthttp
.
StatusOK
{
if
statusCode
!=
fasthttp
.
StatusOK
&&
statusCode
!=
fasthttp
.
StatusPartialContent
{
log
.
WithFields
(
log
.
Fields
{
"expected"
:
f
asthttp
.
StatusOK
,
"expected"
:
f
mt
.
Sprintf
(
"%v,%v"
,
fasthttp
.
StatusOK
,
fasthttp
.
StatusPartialContent
)
,
"got"
:
statusCode
,
})
.
Warn
(
"unexpected status code while retrieving file from SeaweedFS volume server"
)
ctx
.
SetStatusCode
(
fasthttp
.
StatusInternalServerError
)
...
...
@@ -342,12 +353,25 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
return
}
// Set response headers
ctx
.
SetStatusCode
(
statusCode
)
ctx
.
Response
.
Header
.
Set
(
"Accept-Ranges"
,
"bytes"
)
if
contentType
.
Valid
{
ctx
.
SetContentType
(
contentType
.
String
)
}
else
{
ctx
.
SetContentType
(
"application/octet-stream"
)
}
ctx
.
Response
.
Header
.
SetContentLength
(
contentSize
)
if
contentLength
,
ok
:=
resHeaders
[
"Content-Length"
];
ok
{
if
val
,
err
:=
strconv
.
Atoi
(
string
(
contentLength
));
err
!=
nil
&&
val
>=
0
{
ctx
.
Response
.
Header
.
SetContentLength
(
val
)
}
}
if
contentEncoding
,
ok
:=
resHeaders
[
"Content-Encoding"
];
ok
{
ctx
.
Response
.
Header
.
SetBytesV
(
"Content-Encoding"
,
contentEncoding
)
}
if
contentRange
,
ok
:=
resHeaders
[
"Content-Range"
];
ok
{
ctx
.
Response
.
Header
.
SetBytesV
(
"Content-Range"
,
contentRange
)
}
recordMetrics
(
ctx
,
metricsObjectType
)
case
1
:
// redirect
...
...
This diff is collapsed.
Click to expand it.
weed/seaweed.go
+
25
−
12
View file @
655fac38
...
...
@@ -15,15 +15,15 @@ import (
// lookupResponse represents a volume lookup response from the SeaweedFS master.
type
lookupResponse
struct
{
VolumeI
d
string
`json:"volumeId"`
VolumeI
D
string
`json:"volumeId"`
Locations
[]
struct
{
U
rl
string
`json:"url"`
U
RL
string
`json:"url"`
PublicURL
string
`json:"publicUrl"`
}
`json:"locations"`
}
// publicUrlsToSlice converts the list of locations in the lookupResponse to a slice of publicU
rl
strings.
func
(
res
*
lookupResponse
)
publicU
rl
sToSlice
()
[]
string
{
// publicUrlsToSlice converts the list of locations in the lookupResponse to a slice of publicU
RL
strings.
func
(
res
*
lookupResponse
)
publicU
RL
sToSlice
()
[]
string
{
var
urls
=
[]
string
{}
for
_
,
loc
:=
range
res
.
Locations
{
urls
=
append
(
urls
,
loc
.
PublicURL
)
...
...
@@ -55,10 +55,11 @@ func New(masterURI string, lookupTimeout time.Duration) *Seaweed {
}
}
func
(
s
*
Seaweed
)
Get
(
writer
io
.
Writer
,
fid
string
,
query
string
)
(
int
,
int
,
error
)
{
// Get a file from a SeaweedFS cluster.
func
(
s
*
Seaweed
)
Get
(
writer
io
.
Writer
,
fid
string
,
headers
map
[
string
][]
byte
,
query
string
)
(
int
,
map
[
string
][]
byte
,
error
)
{
volumeURL
:=
s
.
lookupVolume
(
strings
.
Split
(
fid
,
","
)[
0
])
if
volumeURL
==
""
{
return
fasthttp
.
StatusInternalServerError
,
0
,
errors
.
New
(
"failed to retrieve volume URL"
)
return
fasthttp
.
StatusInternalServerError
,
nil
,
errors
.
New
(
"failed to retrieve volume URL"
)
}
requestURL
:=
volumeURL
if
!
strings
.
HasPrefix
(
requestURL
,
"http://"
)
&&
!
strings
.
HasPrefix
(
requestURL
,
"https://"
)
{
...
...
@@ -76,6 +77,11 @@ func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, int, err
req
:=
fasthttp
.
AcquireRequest
()
req
.
Reset
()
req
.
SetRequestURI
(
requestURL
)
if
headers
!=
nil
{
for
h
,
v
:=
range
headers
{
req
.
Header
.
SetBytesV
(
h
,
v
)
}
}
res
:=
fasthttp
.
AcquireResponse
()
defer
func
()
{
fasthttp
.
ReleaseRequest
(
req
)
...
...
@@ -85,17 +91,24 @@ func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, int, err
// Perform request
err
:=
fasthttp
.
Do
(
req
,
res
)
if
err
!=
nil
{
return
0
,
0
,
err
return
0
,
nil
,
err
}
if
res
.
StatusCode
()
==
fasthttp
.
StatusOK
{
if
res
.
StatusCode
()
==
fasthttp
.
StatusOK
||
res
.
StatusCode
()
==
fasthttp
.
StatusPartialContent
{
if
err
:=
res
.
BodyWriteTo
(
writer
);
err
!=
nil
{
log
.
WithField
(
"err"
,
err
)
.
Warn
(
"failed to set body writer for response"
)
return
fasthttp
.
StatusInternalServerError
,
0
,
err
return
fasthttp
.
StatusInternalServerError
,
nil
,
err
}
}
return
fasthttp
.
StatusOK
,
res
.
Header
.
ContentLength
(),
err
// Get response headers
resHeaders
:=
map
[
string
][]
byte
{}
res
.
Header
.
VisitAll
(
func
(
key
,
value
[]
byte
)
{
resHeaders
[
string
(
key
)]
=
value
})
return
res
.
StatusCode
(),
resHeaders
,
err
}
// Ping the master of a SeaweedFS cluster (sends a /cluster/status HTTP request to the master).
func
(
s
*
Seaweed
)
Ping
()
error
{
lookupURL
:=
fmt
.
Sprintf
(
"%s/cluster/status"
,
s
.
Master
)
statusCode
,
_
,
err
:=
fasthttp
.
GetTimeout
(
nil
,
lookupURL
,
s
.
LookupTimeout
)
...
...
@@ -103,7 +116,7 @@ func (s *Seaweed) Ping() error {
return
err
}
if
statusCode
!=
fasthttp
.
StatusOK
{
return
errors
.
New
(
fmt
.
Sprint
f
(
"expected 200 OK response status code, got %v"
,
statusCode
)
)
return
fmt
.
Error
f
(
"expected 200 OK response status code, got %v"
,
statusCode
)
}
return
nil
}
...
...
@@ -155,6 +168,6 @@ func (s *Seaweed) lookupVolume(volumeID string) string {
log
.
Warn
(
"SeaweedFS master returned no volume servers without 404ing"
)
return
""
}
s
.
VolumeCache
.
Add
(
volumeUint32
,
res
.
publicU
rl
sToSlice
())
s
.
VolumeCache
.
Add
(
volumeUint32
,
res
.
publicU
RL
sToSlice
())
return
s
.
VolumeCache
.
GetNext
(
volumeUint32
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment