@@ -14,6 +14,7 @@ import (
1414 "os"
1515 "path/filepath"
1616 "time"
17+ "strconv"
1718)
1819
1920const (
5253 IsEnd () bool
5354 Redirect (code int , targetUrl string ) error
5455 QueryString (key string ) string
56+ QueryInt (key string ) int
57+ QueryInt64 (key string ) int64
5558 FormValue (key string ) string
5659 PostFormValue (key string ) string
5760 File (file string ) (err error )
@@ -70,18 +73,18 @@ type (
7073 View (name string ) error
7174 ViewC (code int , name string ) error
7275 Write (code int , content []byte ) (int , error )
73- WriteString (contents ... interface {}) ( int , error )
74- WriteStringC (code int , contents ... interface {}) ( int , error )
75- WriteHtml (contents ... interface {}) ( int , error )
76- WriteHtmlC (code int , contents ... interface {}) ( int , error )
77- WriteBlob (contentType string , b []byte ) ( int , error )
78- WriteBlobC (code int , contentType string , b []byte ) ( int , error )
79- WriteJson (i interface {}) ( int , error )
80- WriteJsonC (code int , i interface {}) ( int , error )
81- WriteJsonBlob (b []byte ) ( int , error )
82- WriteJsonBlobC (code int , b []byte ) ( int , error )
83- WriteJsonp (callback string , i interface {}) ( int , error )
84- WriteJsonpBlob (callback string , b []byte ) ( size int , err error )
76+ WriteString (contents ... interface {}) error
77+ WriteStringC (code int , contents ... interface {}) error
78+ WriteHtml (contents ... interface {}) error
79+ WriteHtmlC (code int , contents ... interface {}) error
80+ WriteBlob (contentType string , b []byte ) error
81+ WriteBlobC (code int , contentType string , b []byte ) error
82+ WriteJson (i interface {}) error
83+ WriteJsonC (code int , i interface {}) error
84+ WriteJsonBlob (b []byte ) error
85+ WriteJsonBlobC (code int , b []byte ) error
86+ WriteJsonp (callback string , i interface {}) error
87+ WriteJsonpBlob (callback string , b []byte ) error
8588 }
8689
8790 HttpContext struct {
@@ -320,6 +323,35 @@ func (ctx *HttpContext) QueryString(key string) string {
320323 return ctx .request .QueryString (key )
321324}
322325
326+ // QueryInt get query key with int format
327+ // if not exists or not int type, return 0
328+ func (ctx * HttpContext ) QueryInt (key string ) int {
329+ param := ctx .request .QueryString (key )
330+ if param == "" {
331+ return 0
332+ }
333+ val , err := strconv .Atoi (param )
334+ if err != nil {
335+ return 0
336+ }
337+ return val
338+ }
339+
340+ // QueryInt64 get query key with int64 format
341+ // if not exists or not int64 type, return 0
342+ func (ctx * HttpContext ) QueryInt64 (key string ) int64 {
343+ param := ctx .request .QueryString (key )
344+ if param == "" {
345+ return 0
346+ }
347+ val , err := strconv .ParseInt (param , 10 , 64 )
348+ if err != nil {
349+ return 0
350+ }
351+ return val
352+ }
353+
354+
323355/*
324356* 根据指定key获取包括在post、put和get内的值
325357 */
@@ -484,94 +516,97 @@ func (ctx *HttpContext) Write(code int, content []byte) (int, error) {
484516}
485517
486518// WriteString write (200, string, text/plain) to response
487- func (ctx * HttpContext ) WriteString (contents ... interface {}) ( int , error ) {
519+ func (ctx * HttpContext ) WriteString (contents ... interface {}) error {
488520 return ctx .WriteStringC (defaultHttpCode , contents ... )
489521}
490522
491523// WriteStringC write (httpCode, string, text/plain) to response
492- func (ctx * HttpContext ) WriteStringC (code int , contents ... interface {}) ( int , error ) {
524+ func (ctx * HttpContext ) WriteStringC (code int , contents ... interface {}) error {
493525 content := fmt .Sprint (contents ... )
494526 return ctx .WriteBlobC (code , MIMETextPlainCharsetUTF8 , []byte (content ))
495527}
496528
497529// WriteString write (200, string, text/html) to response
498- func (ctx * HttpContext ) WriteHtml (contents ... interface {}) ( int , error ) {
530+ func (ctx * HttpContext ) WriteHtml (contents ... interface {}) error {
499531 return ctx .WriteHtmlC (defaultHttpCode , contents ... )
500532}
501533
502534// WriteHtmlC write (httpCode, string, text/html) to response
503- func (ctx * HttpContext ) WriteHtmlC (code int , contents ... interface {}) ( int , error ) {
535+ func (ctx * HttpContext ) WriteHtmlC (code int , contents ... interface {}) error {
504536 content := fmt .Sprint (contents ... )
505537 return ctx .WriteBlobC (code , MIMETextHTMLCharsetUTF8 , []byte (content ))
506538}
507539
508540// WriteBlob write []byte content to response
509- func (ctx * HttpContext ) WriteBlob (contentType string , b []byte ) ( int , error ) {
541+ func (ctx * HttpContext ) WriteBlob (contentType string , b []byte ) error {
510542 return ctx .WriteBlobC (defaultHttpCode , contentType , b )
511543}
512544
513545// WriteBlobC write (httpCode, []byte) to response
514- func (ctx * HttpContext ) WriteBlobC (code int , contentType string , b []byte ) ( int , error ) {
546+ func (ctx * HttpContext ) WriteBlobC (code int , contentType string , b []byte ) error {
515547 if contentType != "" {
516548 ctx .response .SetContentType (contentType )
517549 }
518550 if ctx .IsHijack () {
519- return ctx .hijackConn .WriteBlob (b )
551+ _ , err := ctx .hijackConn .WriteBlob (b )
552+ return err
520553 } else {
521- return ctx .response .Write (code , b )
554+ _ , err := ctx .response .Write (code , b )
555+ return err
522556 }
523557}
524558
525559// WriteJson write (httpCode, json string) to response
526560// auto convert interface{} to json string
527- func (ctx * HttpContext ) WriteJson (i interface {}) ( int , error ) {
561+ func (ctx * HttpContext ) WriteJson (i interface {}) error {
528562 return ctx .WriteJsonC (defaultHttpCode , i )
529563}
530564
531565// WriteJsonC write (httpCode, json string) to response
532566// auto convert interface{} to json string
533- func (ctx * HttpContext ) WriteJsonC (code int , i interface {}) ( int , error ) {
567+ func (ctx * HttpContext ) WriteJsonC (code int , i interface {}) error {
534568 b , err := json .Marshal (i )
535569 if err != nil {
536- return 0 , err
570+ return err
537571 }
538572 return ctx .WriteJsonBlobC (code , b )
539573}
540574
541575// WriteJsonBlob write json []byte to response
542- func (ctx * HttpContext ) WriteJsonBlob (b []byte ) ( int , error ) {
576+ func (ctx * HttpContext ) WriteJsonBlob (b []byte ) error {
543577 return ctx .WriteJsonBlobC (defaultHttpCode , b )
544578}
545579
546580// WriteJsonBlobC write (httpCode, json []byte) to response
547- func (ctx * HttpContext ) WriteJsonBlobC (code int , b []byte ) ( int , error ) {
581+ func (ctx * HttpContext ) WriteJsonBlobC (code int , b []byte ) error {
548582 return ctx .WriteBlobC (code , MIMEApplicationJSONCharsetUTF8 , b )
549583}
550584
551585// WriteJsonp write jsonp string to response
552- func (ctx * HttpContext ) WriteJsonp (callback string , i interface {}) ( int , error ) {
586+ func (ctx * HttpContext ) WriteJsonp (callback string , i interface {}) error {
553587 b , err := json .Marshal (i )
554588 if err != nil {
555- return 0 , err
589+ return err
556590 }
557591 return ctx .WriteJsonpBlob (callback , b )
558592}
559593
560594// WriteJsonpBlob write jsonp string as []byte to response
561- func (ctx * HttpContext ) WriteJsonpBlob (callback string , b []byte ) (size int , err error ) {
595+ func (ctx * HttpContext ) WriteJsonpBlob (callback string , b []byte ) error {
596+ var err error
562597 ctx .response .SetContentType (MIMEApplicationJavaScriptCharsetUTF8 )
563598 //特殊处理,如果为hijack,需要先行WriteBlob头部
564599 if ctx .IsHijack () {
565- if size , err = ctx .hijackConn .WriteBlob ([]byte (ctx .hijackConn .header + "\r \n " )); err != nil {
566- return
600+ if _ , err = ctx .hijackConn .WriteBlob ([]byte (ctx .hijackConn .header + "\r \n " )); err != nil {
601+ return err
567602 }
568603 }
569- if size , err = ctx .WriteBlob ("" , []byte (callback + "(" )); err != nil {
570- return
604+ if err = ctx .WriteBlob ("" , []byte (callback + "(" )); err != nil {
605+ return err
571606 }
572- if size , err = ctx .WriteBlob ("" , b ); err != nil {
573- return
607+ if err = ctx .WriteBlob ("" , b ); err != nil {
608+ return err
574609 }
575- size , err = ctx .WriteBlob ("" , []byte (");" ))
576- return
610+ err = ctx .WriteBlob ("" , []byte (");" ))
611+ return err
577612}
0 commit comments