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
27b7a8f7
Commit
27b7a8f7
authored
Jun 13, 2015
by
MrLee.Kun
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1 from astaxie/develop
Develop
parents
491238ce
c143a6ec
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
136 additions
and
50 deletions
+136
-50
controller.go
controller.go
+35
-0
httplib.go
httplib/httplib.go
+84
-43
db.go
orm/db.go
+2
-0
db_mysql.go
orm/db_mysql.go
+2
-0
db_postgres.go
orm/db_postgres.go
+2
-0
db_sqlite.go
orm/db_sqlite.go
+2
-0
router.go
router.go
+6
-4
router_test.go
router_test.go
+3
-3
No files found.
controller.go
View file @
27b7a8f7
...
...
@@ -499,6 +499,41 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader,
return
c
.
Ctx
.
Request
.
FormFile
(
key
)
}
// GetFiles return multi-upload files
// files, err:=c.Getfiles("myfiles")
// if err != nil {
// http.Error(w, err.Error(), http.StatusNoContent)
// return
// }
// for i, _ := range files {
// //for each fileheader, get a handle to the actual file
// file, err := files[i].Open()
// defer file.Close()
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// //create destination file making sure the path is writeable.
// dst, err := os.Create("upload/" + files[i].Filename)
// defer dst.Close()
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// //copy the uploaded file to the destination file
// if _, err := io.Copy(dst, file); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// }
func
(
c
*
Controller
)
GetFiles
(
key
string
)
([]
*
multipart
.
FileHeader
,
error
)
{
files
,
ok
:=
c
.
Ctx
.
Request
.
MultipartForm
.
File
[
"key"
]
if
ok
{
return
files
,
nil
}
return
nil
,
http
.
ErrMissingFile
}
// SaveToFile saves uploaded file to new path.
// it only operates the first one of mutil-upload form file field.
func
(
c
*
Controller
)
SaveToFile
(
fromfile
,
tofile
string
)
error
{
...
...
httplib/httplib.go
View file @
27b7a8f7
...
...
@@ -36,7 +36,6 @@ import (
"crypto/tls"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
...
...
@@ -48,26 +47,50 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"
)
var
defaultSetting
=
BeegoHttpSettings
{
UserAgent
:
"beegoServer"
,
ConnectTimeout
:
60
*
time
.
Second
,
ReadWriteTimeout
:
60
*
time
.
Second
,
Gzip
:
true
}
var
defaultSetting
=
BeegoHttpSettings
{
UserAgent
:
"beegoServer"
,
ConnectTimeout
:
60
*
time
.
Second
,
ReadWriteTimeout
:
60
*
time
.
Second
,
Gzip
:
true
,
DumpBody
:
true
,
}
var
defaultCookieJar
http
.
CookieJar
var
settingMutex
sync
.
Mutex
// createDefaultCookie creates a global cookiejar to store cookies.
func
createDefaultCookie
()
{
settingMutex
.
Lock
()
defer
settingMutex
.
Unlock
()
defaultCookieJar
,
_
=
cookiejar
.
New
(
nil
)
}
// Overwrite default settings
func
SetDefaultSetting
(
setting
BeegoHttpSettings
)
{
settingMutex
.
Lock
()
defer
settingMutex
.
Unlock
()
defaultSetting
=
setting
if
defaultSetting
.
ConnectTimeout
==
0
{
defaultSetting
.
ConnectTimeout
=
60
*
time
.
Second
}
if
defaultSetting
.
ReadWriteTimeout
==
0
{
defaultSetting
.
ReadWriteTimeout
=
60
*
time
.
Second
}
}
// return *BeegoHttpRequest with specific method
func
newBeegoRequest
(
url
,
method
string
)
*
BeegoHttpRequest
{
func
NewBeegoRequest
(
raw
url
,
method
string
)
*
BeegoHttpRequest
{
var
resp
http
.
Response
u
,
err
:=
url
.
Parse
(
rawurl
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
req
:=
http
.
Request
{
URL
:
u
,
Method
:
method
,
Header
:
make
(
http
.
Header
),
Proto
:
"HTTP/1.1"
,
...
...
@@ -75,7 +98,7 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest {
ProtoMinor
:
1
,
}
return
&
BeegoHttpRequest
{
url
:
url
,
url
:
raw
url
,
req
:
&
req
,
params
:
map
[
string
]
string
{},
files
:
map
[
string
]
string
{},
...
...
@@ -86,27 +109,27 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest {
// Get returns *BeegoHttpRequest with GET method.
func
Get
(
url
string
)
*
BeegoHttpRequest
{
return
n
ewBeegoRequest
(
url
,
"GET"
)
return
N
ewBeegoRequest
(
url
,
"GET"
)
}
// Post returns *BeegoHttpRequest with POST method.
func
Post
(
url
string
)
*
BeegoHttpRequest
{
return
n
ewBeegoRequest
(
url
,
"POST"
)
return
N
ewBeegoRequest
(
url
,
"POST"
)
}
// Put returns *BeegoHttpRequest with PUT method.
func
Put
(
url
string
)
*
BeegoHttpRequest
{
return
n
ewBeegoRequest
(
url
,
"PUT"
)
return
N
ewBeegoRequest
(
url
,
"PUT"
)
}
// Delete returns *BeegoHttpRequest DELETE method.
func
Delete
(
url
string
)
*
BeegoHttpRequest
{
return
n
ewBeegoRequest
(
url
,
"DELETE"
)
return
N
ewBeegoRequest
(
url
,
"DELETE"
)
}
// Head returns *BeegoHttpRequest with HEAD method.
func
Head
(
url
string
)
*
BeegoHttpRequest
{
return
n
ewBeegoRequest
(
url
,
"HEAD"
)
return
N
ewBeegoRequest
(
url
,
"HEAD"
)
}
// BeegoHttpSettings
...
...
@@ -120,6 +143,7 @@ type BeegoHttpSettings struct {
Transport
http
.
RoundTripper
EnableCookie
bool
Gzip
bool
DumpBody
bool
}
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
...
...
@@ -134,6 +158,11 @@ type BeegoHttpRequest struct {
dump
[]
byte
}
// get request
func
(
b
*
BeegoHttpRequest
)
GetRequest
()
*
http
.
Request
{
return
b
.
req
}
// Change request settings
func
(
b
*
BeegoHttpRequest
)
Setting
(
setting
BeegoHttpSettings
)
*
BeegoHttpRequest
{
b
.
setting
=
setting
...
...
@@ -153,14 +182,20 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest {
}
// SetUserAgent sets User-Agent header field
func
(
b
*
BeegoHttpRequest
)
SetUserAgent
(
user
A
gent
string
)
*
BeegoHttpRequest
{
b
.
setting
.
UserAgent
=
user
A
gent
func
(
b
*
BeegoHttpRequest
)
SetUserAgent
(
user
a
gent
string
)
*
BeegoHttpRequest
{
b
.
setting
.
UserAgent
=
user
a
gent
return
b
}
// Debug sets show debug or not when executing request.
func
(
b
*
BeegoHttpRequest
)
Debug
(
isDebug
bool
)
*
BeegoHttpRequest
{
b
.
setting
.
ShowDebug
=
isDebug
func
(
b
*
BeegoHttpRequest
)
Debug
(
isdebug
bool
)
*
BeegoHttpRequest
{
b
.
setting
.
ShowDebug
=
isdebug
return
b
}
// Dump Body.
func
(
b
*
BeegoHttpRequest
)
DumpBody
(
isdump
bool
)
*
BeegoHttpRequest
{
b
.
setting
.
DumpBody
=
isdump
return
b
}
...
...
@@ -279,21 +314,18 @@ func (b *BeegoHttpRequest) JsonBody(obj interface{}) (*BeegoHttpRequest, error)
}
func
(
b
*
BeegoHttpRequest
)
buildUrl
(
paramBody
string
)
{
if
paramBody
==
""
{
return
}
// build GET url with query string
if
b
.
req
.
Method
==
"GET"
{
if
strings
.
Index
(
b
.
url
,
"?"
)
==
-
1
{
b
.
url
=
b
.
url
+
"?"
+
paramBody
}
else
{
if
b
.
req
.
Method
==
"GET"
&&
len
(
paramBody
)
>
0
{
if
strings
.
Index
(
b
.
url
,
"?"
)
!=
-
1
{
b
.
url
+=
"&"
+
paramBody
}
else
{
b
.
url
=
b
.
url
+
"?"
+
paramBody
}
return
}
// build POST url and body
if
b
.
req
.
Method
==
"POST"
&&
b
.
req
.
Body
==
nil
{
// build POST
/PUT/PATCH
url and body
if
(
b
.
req
.
Method
==
"POST"
||
b
.
req
.
Method
==
"PUT"
||
b
.
req
.
Method
==
"PATCH"
)
&&
b
.
req
.
Body
==
nil
{
// with files
if
len
(
b
.
files
)
>
0
{
pr
,
pw
:=
io
.
Pipe
()
...
...
@@ -338,16 +370,29 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if
b
.
resp
.
StatusCode
!=
0
{
return
b
.
resp
,
nil
}
resp
,
err
:=
b
.
SendOut
()
if
err
!=
nil
{
return
nil
,
err
}
b
.
resp
=
resp
return
resp
,
nil
}
func
(
b
*
BeegoHttpRequest
)
SendOut
()
(
*
http
.
Response
,
error
)
{
var
paramBody
string
if
len
(
b
.
params
)
>
0
{
var
buf
bytes
.
Buffer
for
k
,
v
:=
range
b
.
params
{
paramBody
+=
fmt
.
Sprintf
(
"&%s=%v"
,
url
.
QueryEscape
(
k
),
url
.
QueryEscape
(
v
))
buf
.
WriteString
(
url
.
QueryEscape
(
k
))
buf
.
WriteByte
(
'='
)
buf
.
WriteString
(
url
.
QueryEscape
(
v
))
buf
.
WriteByte
(
'&'
)
}
paramBody
=
paramBody
[
1
:
]
paramBody
=
buf
.
String
()
paramBody
=
paramBody
[
0
:
len
(
paramBody
)
-
1
]
}
b
.
buildUrl
(
paramBody
)
url
,
err
:=
url
.
Parse
(
b
.
url
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -357,13 +402,6 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
trans
:=
b
.
setting
.
Transport
if
b
.
setting
.
ConnectTimeout
==
0
{
b
.
setting
.
ConnectTimeout
=
60
*
time
.
Second
}
if
b
.
setting
.
ReadWriteTimeout
==
0
{
b
.
setting
.
ReadWriteTimeout
=
60
*
time
.
Second
}
if
trans
==
nil
{
// create default transport
trans
=
&
http
.
Transport
{
...
...
@@ -404,15 +442,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}
if
b
.
setting
.
ShowDebug
{
dump
,
err
:=
httputil
.
DumpRequest
(
b
.
req
,
true
)
dump
,
err
:=
httputil
.
DumpRequest
(
b
.
req
,
b
.
setting
.
DumpBody
)
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
}
b
.
dump
=
dump
}
b
.
resp
,
err
=
client
.
Do
(
b
.
req
)
return
b
.
resp
,
err
return
client
.
Do
(
b
.
req
)
}
// String returns the body string in response.
...
...
@@ -433,9 +469,12 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
return
b
.
body
,
nil
}
resp
,
err
:=
b
.
getResponse
()
if
resp
==
nil
||
resp
.
Body
=
=
nil
{
if
err
!
=
nil
{
return
nil
,
err
}
if
resp
.
Body
==
nil
{
return
nil
,
nil
}
defer
resp
.
Body
.
Close
()
if
b
.
setting
.
Gzip
&&
resp
.
Header
.
Get
(
"Content-Encoding"
)
==
"gzip"
{
reader
,
err
:=
gzip
.
NewReader
(
resp
.
Body
)
...
...
@@ -452,18 +491,20 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
// ToFile saves the body data in response to one file.
// it calls Response inner.
func
(
b
*
BeegoHttpRequest
)
ToFile
(
filename
string
)
error
{
resp
,
err
:=
b
.
getResponse
()
if
resp
==
nil
||
resp
.
Body
==
nil
{
return
err
}
defer
resp
.
Body
.
Close
()
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
resp
,
err
:=
b
.
getResponse
()
if
err
!=
nil
{
return
err
}
if
resp
.
Body
==
nil
{
return
nil
}
defer
resp
.
Body
.
Close
()
_
,
err
=
io
.
Copy
(
f
,
resp
.
Body
)
return
err
}
...
...
orm/db.go
View file @
27b7a8f7
...
...
@@ -44,6 +44,8 @@ var (
"gte"
:
true
,
"lt"
:
true
,
"lte"
:
true
,
"eq"
:
true
,
"nq"
:
true
,
"startswith"
:
true
,
"endswith"
:
true
,
"istartswith"
:
true
,
...
...
orm/db_mysql.go
View file @
27b7a8f7
...
...
@@ -30,6 +30,8 @@ var mysqlOperators = map[string]string{
"gte"
:
">= ?"
,
"lt"
:
"< ?"
,
"lte"
:
"<= ?"
,
"eq"
:
"= ?"
,
"ne"
:
"!= ?"
,
"startswith"
:
"LIKE BINARY ?"
,
"endswith"
:
"LIKE BINARY ?"
,
"istartswith"
:
"LIKE ?"
,
...
...
orm/db_postgres.go
View file @
27b7a8f7
...
...
@@ -29,6 +29,8 @@ var postgresOperators = map[string]string{
"gte"
:
">= ?"
,
"lt"
:
"< ?"
,
"lte"
:
"<= ?"
,
"eq"
:
"= ?"
,
"ne"
:
"!= ?"
,
"startswith"
:
"LIKE ?"
,
"endswith"
:
"LIKE ?"
,
"istartswith"
:
"LIKE UPPER(?)"
,
...
...
orm/db_sqlite.go
View file @
27b7a8f7
...
...
@@ -29,6 +29,8 @@ var sqliteOperators = map[string]string{
"gte"
:
">= ?"
,
"lt"
:
"< ?"
,
"lte"
:
"<= ?"
,
"eq"
:
"= ?"
,
"ne"
:
"!= ?"
,
"startswith"
:
"LIKE ? ESCAPE '
\\
'"
,
"endswith"
:
"LIKE ? ESCAPE '
\\
'"
,
"istartswith"
:
"LIKE ? ESCAPE '
\\
'"
,
...
...
router.go
View file @
27b7a8f7
...
...
@@ -611,19 +611,21 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
pos
];
ok
{
for
_
,
filterR
:=
range
l
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
urlPath
);
ok
{
for
k
,
v
:=
range
p
{
if
filterR
.
returnOnOutput
&&
w
.
started
{
return
true
}
if
ok
,
params
:=
filterR
.
ValidRouter
(
urlPath
);
ok
{
for
k
,
v
:=
range
params
{
context
.
Input
.
Params
[
k
]
=
v
}
filterR
.
filterFunc
(
context
)
}
if
filterR
.
returnOnOutput
&&
w
.
started
{
return
true
}
}
}
}
}
return
false
}
...
...
router_test.go
View file @
27b7a8f7
...
...
@@ -444,7 +444,7 @@ func TestFilterAfterExec(t *testing.T) {
mux
:=
NewControllerRegister
()
mux
.
InsertFilter
(
url
,
BeforeRouter
,
beegoFilterNoOutput
)
mux
.
InsertFilter
(
url
,
BeforeExec
,
beegoFilterNoOutput
)
mux
.
InsertFilter
(
url
,
AfterExec
,
beegoAfterExec1
)
mux
.
InsertFilter
(
url
,
AfterExec
,
beegoAfterExec1
,
false
)
mux
.
Get
(
url
,
beegoFilterFunc
)
...
...
@@ -506,7 +506,7 @@ func TestFilterFinishRouterMultiFirstOnly(t *testing.T) {
url
:=
"/finishRouterMultiFirstOnly"
mux
:=
NewControllerRegister
()
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter1
)
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter1
,
false
)
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter2
)
mux
.
Get
(
url
,
beegoFilterFunc
)
...
...
@@ -534,7 +534,7 @@ func TestFilterFinishRouterMulti(t *testing.T) {
mux
:=
NewControllerRegister
()
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter1
,
false
)
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter2
)
mux
.
InsertFilter
(
url
,
FinishRouter
,
beegoFinishRouter2
,
false
)
mux
.
Get
(
url
,
beegoFilterFunc
)
...
...
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