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
f1e50596
Commit
f1e50596
authored
Jul 08, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #69 refer to
http://www.php.net/manual/zh/function.setcookie.php
parent
11977f4f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
10 deletions
+42
-10
context.go
context.go
+42
-10
No files found.
context.go
View file @
f1e50596
package
beego
package
beego
import
(
import
(
"bytes"
"fmt"
"fmt"
"mime"
"mime"
"net/http"
"net/http"
"strings"
"strings"
"time"
)
)
type
Context
struct
{
type
Context
struct
{
...
@@ -37,7 +37,7 @@ func (ctx *Context) NotFound(message string) {
...
@@ -37,7 +37,7 @@ func (ctx *Context) NotFound(message string) {
ctx
.
ResponseWriter
.
Write
([]
byte
(
message
))
ctx
.
ResponseWriter
.
Write
([]
byte
(
message
))
}
}
//Sets the content type by extension, as defined in the mime package.
//Sets the content type by extension, as defined in the mime package.
//For example, ctx.ContentType("json") sets the content-type to "application/json"
//For example, ctx.ContentType("json") sets the content-type to "application/json"
func
(
ctx
*
Context
)
ContentType
(
ext
string
)
{
func
(
ctx
*
Context
)
ContentType
(
ext
string
)
{
if
!
strings
.
HasPrefix
(
ext
,
"."
)
{
if
!
strings
.
HasPrefix
(
ext
,
"."
)
{
...
@@ -58,14 +58,46 @@ func (ctx *Context) SetHeader(hdr string, val string, unique bool) {
...
@@ -58,14 +58,46 @@ func (ctx *Context) SetHeader(hdr string, val string, unique bool) {
}
}
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
func
(
ctx
*
Context
)
SetCookie
(
name
string
,
value
string
,
age
int64
)
{
var
utctime
time
.
Time
//params:
if
age
==
0
{
//string name
// 2^31 - 1 seconds (roughly 2038)
//string value
utctime
=
time
.
Unix
(
2147483647
,
0
)
//int64 expire = 0
//string $path
//string $domain
//bool $secure = false
//bool $httponly = false
func
(
ctx
*
Context
)
SetCookie
(
name
string
,
value
string
,
others
...
interface
{})
{
var
b
bytes
.
Buffer
fmt
.
Fprintf
(
&
b
,
"%s=%s"
,
sanitizeName
(
name
),
sanitizeValue
(
value
))
if
len
(
others
)
>
0
{
fmt
.
Fprintf
(
&
b
,
"; Max-Age=%d"
,
others
[
0
]
.
(
int64
))
}
else
{
}
else
{
utctime
=
time
.
Unix
(
time
.
Now
()
.
Unix
()
+
age
,
0
)
fmt
.
Fprintf
(
&
b
,
"; Max-Age=0"
)
}
if
len
(
others
)
>
1
{
fmt
.
Fprintf
(
&
b
,
"; Path=%s"
,
sanitizeValue
(
others
[
1
]
.
(
string
)))
}
if
len
(
others
)
>
2
{
fmt
.
Fprintf
(
&
b
,
"; Domain=%s"
,
sanitizeValue
(
others
[
2
]
.
(
string
)))
}
}
cookie
:=
fmt
.
Sprintf
(
"%s=%s; Expires=%s; Path=/"
,
name
,
value
,
webTime
(
utctime
))
if
len
(
others
)
>
3
{
ctx
.
SetHeader
(
"Set-Cookie"
,
cookie
,
true
)
fmt
.
Fprintf
(
&
b
,
"; Secure"
)
}
if
len
(
others
)
>
4
{
fmt
.
Fprintf
(
&
b
,
"; HttpOnly"
)
}
ctx
.
SetHeader
(
"Set-Cookie"
,
b
.
String
(),
true
)
}
var
cookieNameSanitizer
=
strings
.
NewReplacer
(
"
\n
"
,
"-"
,
"
\r
"
,
"-"
)
func
sanitizeName
(
n
string
)
string
{
return
cookieNameSanitizer
.
Replace
(
n
)
}
var
cookieValueSanitizer
=
strings
.
NewReplacer
(
"
\n
"
,
" "
,
"
\r
"
,
" "
,
";"
,
" "
)
func
sanitizeValue
(
v
string
)
string
{
return
cookieValueSanitizer
.
Replace
(
v
)
}
}
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