Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
fdccd853
Unverified
Commit
fdccd853
authored
Jul 20, 2018
by
astaxie
Committed by
GitHub
Jul 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3152 from joshtechnologygroup/staticfile_unexpected_eof_bug_gix
[#2973] Fix Unexpected EOF bug in staticfile
parents
ca394fc8
98a3cda2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
13 deletions
+19
-13
staticfile.go
staticfile.go
+15
-9
staticfile_test.go
staticfile_test.go
+4
-4
No files found.
staticfile.go
View file @
fdccd853
...
...
@@ -74,7 +74,7 @@ func serverStaticRouter(ctx *context.Context) {
if
enableCompress
{
acceptEncoding
=
context
.
ParseEncoding
(
ctx
.
Request
)
}
b
,
n
,
sch
,
err
:=
openFile
(
filePath
,
fileInfo
,
acceptEncoding
)
b
,
n
,
sch
,
reader
,
err
:=
openFile
(
filePath
,
fileInfo
,
acceptEncoding
)
if
err
!=
nil
{
if
BConfig
.
RunMode
==
DEV
{
logs
.
Warn
(
"Can't compress the file:"
,
filePath
,
err
)
...
...
@@ -89,47 +89,53 @@ func serverStaticRouter(ctx *context.Context) {
ctx
.
Output
.
Header
(
"Content-Length"
,
strconv
.
FormatInt
(
sch
.
size
,
10
))
}
http
.
ServeContent
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
,
sch
.
modTime
,
sch
)
http
.
ServeContent
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
,
sch
.
modTime
,
reader
)
}
type
serveContentHolder
struct
{
*
bytes
.
Reader
data
[]
byte
modTime
time
.
Time
size
int64
encoding
string
}
type
serveContentReader
struct
{
*
bytes
.
Reader
}
var
(
staticFileMap
=
make
(
map
[
string
]
*
serveContentHolder
)
mapLock
sync
.
RWMutex
)
func
openFile
(
filePath
string
,
fi
os
.
FileInfo
,
acceptEncoding
string
)
(
bool
,
string
,
*
serveContentHolder
,
error
)
{
func
openFile
(
filePath
string
,
fi
os
.
FileInfo
,
acceptEncoding
string
)
(
bool
,
string
,
*
serveContentHolder
,
*
serveContentReader
,
error
)
{
mapKey
:=
acceptEncoding
+
":"
+
filePath
mapLock
.
RLock
()
mapFile
:=
staticFileMap
[
mapKey
]
mapLock
.
RUnlock
()
if
isOk
(
mapFile
,
fi
)
{
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
nil
reader
:=
&
serveContentReader
{
Reader
:
bytes
.
NewReader
(
mapFile
.
data
)}
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
reader
,
nil
}
mapLock
.
Lock
()
defer
mapLock
.
Unlock
()
if
mapFile
=
staticFileMap
[
mapKey
];
!
isOk
(
mapFile
,
fi
)
{
file
,
err
:=
os
.
Open
(
filePath
)
if
err
!=
nil
{
return
false
,
""
,
nil
,
err
return
false
,
""
,
nil
,
nil
,
err
}
defer
file
.
Close
()
var
bufferWriter
bytes
.
Buffer
_
,
n
,
err
:=
context
.
WriteFile
(
acceptEncoding
,
&
bufferWriter
,
file
)
if
err
!=
nil
{
return
false
,
""
,
nil
,
err
return
false
,
""
,
nil
,
nil
,
err
}
mapFile
=
&
serveContentHolder
{
Reader
:
bytes
.
NewReader
(
bufferWriter
.
Bytes
()
),
modTime
:
fi
.
ModTime
(),
size
:
int64
(
bufferWriter
.
Len
()),
encoding
:
n
}
mapFile
=
&
serveContentHolder
{
data
:
bufferWriter
.
Bytes
(
),
modTime
:
fi
.
ModTime
(),
size
:
int64
(
bufferWriter
.
Len
()),
encoding
:
n
}
staticFileMap
[
mapKey
]
=
mapFile
}
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
nil
reader
:=
&
serveContentReader
{
Reader
:
bytes
.
NewReader
(
mapFile
.
data
)}
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
reader
,
nil
}
func
isOk
(
s
*
serveContentHolder
,
fi
os
.
FileInfo
)
bool
{
...
...
staticfile_test.go
View file @
fdccd853
...
...
@@ -16,7 +16,7 @@ var licenseFile = filepath.Join(currentWorkDir, "LICENSE")
func
testOpenFile
(
encoding
string
,
content
[]
byte
,
t
*
testing
.
T
)
{
fi
,
_
:=
os
.
Stat
(
licenseFile
)
b
,
n
,
sch
,
err
:=
openFile
(
licenseFile
,
fi
,
encoding
)
b
,
n
,
sch
,
reader
,
err
:=
openFile
(
licenseFile
,
fi
,
encoding
)
if
err
!=
nil
{
t
.
Log
(
err
)
t
.
Fail
()
...
...
@@ -24,7 +24,7 @@ func testOpenFile(encoding string, content []byte, t *testing.T) {
t
.
Log
(
"open static file encoding "
+
n
,
b
)
assetOpenFileAndContent
(
sch
,
content
,
t
)
assetOpenFileAndContent
(
sch
,
reader
,
content
,
t
)
}
func
TestOpenStaticFile_1
(
t
*
testing
.
T
)
{
file
,
_
:=
os
.
Open
(
licenseFile
)
...
...
@@ -53,13 +53,13 @@ func TestOpenStaticFileDeflate_1(t *testing.T) {
testOpenFile
(
"deflate"
,
content
,
t
)
}
func
assetOpenFileAndContent
(
sch
*
serveContentHolder
,
content
[]
byte
,
t
*
testing
.
T
)
{
func
assetOpenFileAndContent
(
sch
*
serveContentHolder
,
reader
*
serveContentReader
,
content
[]
byte
,
t
*
testing
.
T
)
{
t
.
Log
(
sch
.
size
,
len
(
content
))
if
sch
.
size
!=
int64
(
len
(
content
))
{
t
.
Log
(
"static content file size not same"
)
t
.
Fail
()
}
bs
,
_
:=
ioutil
.
ReadAll
(
sch
)
bs
,
_
:=
ioutil
.
ReadAll
(
reader
)
for
i
,
v
:=
range
content
{
if
v
!=
bs
[
i
]
{
t
.
Log
(
"content not same"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment