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
d2c60619
Commit
d2c60619
authored
Nov 11, 2015
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new static file support code
parent
82c50b97
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
65 deletions
+115
-65
staticfile.go
staticfile.go
+115
-65
No files found.
staticfile.go
View file @
d2c60619
...
@@ -15,123 +15,173 @@
...
@@ -15,123 +15,173 @@
package
beego
package
beego
import
(
import
(
"bytes"
"net/http"
"net/http"
"os"
"os"
"path"
"path"
"path/filepath"
"path/filepath"
"strconv"
"strconv"
"strings"
"strings"
"sync"
"errors"
"time"
"github.com/astaxie/beego/acceptencoder"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/utils"
)
)
var
notStaticRequestErr
=
errors
.
New
(
"request not a static file request"
)
func
serverStaticRouter
(
ctx
*
context
.
Context
)
{
func
serverStaticRouter
(
ctx
*
context
.
Context
)
{
if
ctx
.
Input
.
Method
()
!=
"GET"
&&
ctx
.
Input
.
Method
()
!=
"HEAD"
{
if
ctx
.
Input
.
Method
()
!=
"GET"
&&
ctx
.
Input
.
Method
()
!=
"HEAD"
{
return
return
}
}
requestPath
:=
filepath
.
Clean
(
ctx
.
Input
.
Request
.
URL
.
Path
)
// special processing : favicon.ico/robots.txt can be in any static dir
forbidden
,
filePath
,
fileInfo
,
err
:=
lookupFile
(
ctx
)
if
requestPath
==
"/favicon.ico"
||
requestPath
==
"/robots.txt"
{
if
err
==
notStaticRequestErr
{
file
:=
path
.
Join
(
"."
,
requestPath
)
if
utils
.
FileExists
(
file
)
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
file
)
return
return
}
}
for
_
,
staticDir
:=
range
StaticDir
{
if
forbidden
{
file
:=
path
.
Join
(
staticDir
,
requestPath
)
exception
(
"403"
,
ctx
)
if
utils
.
FileExists
(
file
)
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
file
)
return
return
}
}
}
if
filePath
==
""
||
fileInfo
==
nil
{
if
RunMode
==
"dev"
{
Warn
(
"Can't find/open the file:"
,
filePath
,
err
)
}
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
return
return
}
}
if
fileInfo
.
IsDir
()
{
for
prefix
,
staticDir
:=
range
StaticDir
{
//serveFile will list dir
if
len
(
prefix
)
==
0
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
)
continue
return
}
}
if
strings
.
HasPrefix
(
requestPath
,
prefix
)
{
if
len
(
requestPath
)
>
len
(
prefix
)
&&
requestPath
[
len
(
prefix
)]
!=
'/'
{
var
enableCompress
=
EnableGzip
&&
isStaticCompress
(
filePath
)
continue
var
acceptEncoding
string
if
enableCompress
{
acceptEncoding
=
context
.
ParseEncoding
(
ctx
.
Request
)
}
}
filePath
:=
path
.
Join
(
staticDir
,
requestPath
[
len
(
prefix
)
:
])
b
,
n
,
sch
,
err
:=
openFile
(
filePath
,
fileInfo
,
acceptEncoding
)
fileInfo
,
err
:=
os
.
Stat
(
filePath
)
if
err
!=
nil
{
if
err
!=
nil
{
if
RunMode
==
"dev"
{
if
RunMode
==
"dev"
{
Warn
(
"Can't find
the file:"
,
filePath
,
err
)
Warn
(
"Can't compress
the file:"
,
filePath
,
err
)
}
}
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
return
return
}
}
//if the request is dir and DirectoryIndex is false then
if
fileInfo
.
IsDir
()
{
if
b
{
if
!
DirectoryIndex
{
ctx
.
Output
.
Header
(
"Content-Encoding"
,
n
)
exception
(
"403"
,
ctx
)
}
else
{
return
ctx
.
Output
.
Header
(
"Content-Length"
,
strconv
.
FormatInt
(
sch
.
size
,
10
))
}
}
if
ctx
.
Input
.
Request
.
URL
.
Path
[
len
(
ctx
.
Input
.
Request
.
URL
.
Path
)
-
1
]
!=
'/'
{
http
.
Redirect
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
ctx
.
Input
.
Request
.
URL
.
Path
+
"/"
,
302
)
http
.
ServeContent
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
,
sch
.
modTime
,
sch
)
return
return
}
}
if
strings
.
HasSuffix
(
requestPath
,
"/index.html"
)
{
}
fileReader
,
err
:=
os
.
Open
(
filePath
)
type
serveContentHolder
struct
{
*
strings
.
Reader
modTime
time
.
Time
size
int64
encoding
string
}
var
(
staticFileMap
=
make
(
map
[
string
]
*
serveContentHolder
)
mapLock
sync
.
Mutex
)
func
openFile
(
filePath
string
,
fi
os
.
FileInfo
,
acceptEncoding
string
)
(
bool
,
string
,
*
serveContentHolder
,
error
)
{
mapKey
:=
acceptEncoding
+
":"
+
filePath
mapFile
,
ok
:=
staticFileMap
[
mapKey
]
if
ok
{
if
mapFile
.
modTime
==
fi
.
ModTime
()
&&
mapFile
.
size
==
fi
.
Size
()
{
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
nil
}
}
mapLock
.
Lock
()
delete
(
staticFileMap
,
mapKey
)
defer
mapLock
.
Unlock
()
if
mapFile
,
ok
=
staticFileMap
[
mapKey
];
!
ok
{
file
,
err
:=
os
.
Open
(
filePath
)
if
err
!=
nil
{
if
err
!=
nil
{
if
RunMode
==
"dev"
{
return
false
,
""
,
nil
,
err
Warn
(
"Can't open the file:"
,
filePath
,
err
)
}
}
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
defer
file
.
Close
()
return
var
bufferWriter
bytes
.
Buffer
_
,
n
,
err
:=
context
.
WriteFile
(
acceptEncoding
,
&
bufferWriter
,
file
)
if
err
!=
nil
{
return
false
,
""
,
nil
,
err
}
}
defer
fileReader
.
Close
()
mapFile
=
&
serveContentHolder
{
Reader
:
strings
.
NewReader
(
string
(
bufferWriter
.
Bytes
())),
modTime
:
fi
.
ModTime
(),
size
:
int64
(
bufferWriter
.
Len
()),
encoding
:
n
}
http
.
ServeContent
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
,
fileInfo
.
ModTime
(),
fileReader
)
staticFileMap
[
mapKey
]
=
mapFile
return
}
}
isStaticFileToCompress
:=
false
return
mapFile
.
encoding
!=
""
,
mapFile
.
encoding
,
mapFile
,
nil
}
func
isStaticCompress
(
filePath
string
)
bool
{
for
_
,
statExtension
:=
range
StaticExtensionsToGzip
{
for
_
,
statExtension
:=
range
StaticExtensionsToGzip
{
if
strings
.
HasSuffix
(
strings
.
ToLower
(
filePath
),
strings
.
ToLower
(
statExtension
))
{
if
strings
.
HasSuffix
(
strings
.
ToLower
(
filePath
),
strings
.
ToLower
(
statExtension
))
{
isStaticFileToCompress
=
true
return
true
break
}
}
}
}
return
false
}
if
!
isStaticFileToCompress
{
func
searchFile
(
ctx
*
context
.
Context
)
(
string
,
os
.
FileInfo
,
error
)
{
http
.
ServeFile
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
)
requestPath
:=
filepath
.
Clean
(
ctx
.
Input
.
Request
.
URL
.
Path
)
return
// special processing : favicon.ico/robots.txt can be in any static dir
if
requestPath
==
"/favicon.ico"
||
requestPath
==
"/robots.txt"
{
file
:=
path
.
Join
(
"."
,
requestPath
)
if
fi
,
_
:=
os
.
Stat
(
file
);
fi
!=
nil
{
return
file
,
fi
,
nil
}
}
for
_
,
staticDir
:=
range
StaticDir
{
//to compress file
filePath
:=
path
.
Join
(
staticDir
,
requestPath
)
var
contentEncoding
string
if
fi
,
_
:=
os
.
Stat
(
filePath
);
fi
!=
nil
{
if
EnableGzip
{
return
filePath
,
fi
,
nil
contentEncoding
=
acceptencoder
.
ParseEncoding
(
ctx
.
Request
)
}
}
memZipFile
,
err
:=
openMemZipFile
(
filePath
,
contentEncoding
)
if
err
!=
nil
{
if
RunMode
==
"dev"
{
Warn
(
"Can't compress the file:"
,
filePath
,
err
)
}
}
http
.
NotFound
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
return
""
,
nil
,
errors
.
New
(
requestPath
+
" file not find"
)
return
}
}
if
contentEncoding
==
""
{
for
prefix
,
staticDir
:=
range
StaticDir
{
ctx
.
Output
.
Header
(
"Content-Length"
,
strconv
.
FormatInt
(
fileInfo
.
Size
(),
10
))
if
len
(
prefix
)
==
0
{
}
else
{
continue
ctx
.
Output
.
Header
(
"Content-Encoding"
,
contentEncoding
)
}
if
!
strings
.
Contains
(
requestPath
,
prefix
)
{
continue
}
}
if
len
(
requestPath
)
>
len
(
prefix
)
&&
requestPath
[
len
(
prefix
)]
!=
'/'
{
continue
}
filePath
:=
path
.
Join
(
staticDir
,
requestPath
[
len
(
prefix
)
:
])
if
fi
,
err
:=
os
.
Stat
(
filePath
);
fi
!=
nil
{
return
filePath
,
fi
,
err
}
}
return
""
,
nil
,
notStaticRequestErr
}
http
.
ServeContent
(
ctx
.
ResponseWriter
,
ctx
.
Request
,
filePath
,
fileInfo
.
ModTime
(),
memZipFile
)
func
lookupFile
(
ctx
*
context
.
Context
)
(
bool
,
string
,
os
.
FileInfo
,
error
)
{
return
fp
,
fi
,
err
:=
searchFile
(
ctx
)
if
fp
==
""
||
fi
==
nil
{
return
false
,
""
,
nil
,
err
}
if
!
fi
.
IsDir
()
{
return
false
,
fp
,
fi
,
err
}
}
ifp
:=
filepath
.
Join
(
fp
,
"index.html"
)
if
ifi
,
_
:=
os
.
Stat
(
ifp
);
ifi
!=
nil
&&
ifi
.
Mode
()
.
IsRegular
()
{
return
false
,
ifp
,
ifi
,
err
}
}
return
!
DirectoryIndex
,
fp
,
fi
,
err
}
}
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