@@ -96,11 +96,17 @@ type ProxySession struct {
9696}
9797
9898// set the value of the specified key in the JSON body
99- func SetJSONVariable (body []byte , key string , value interface {}) ([]byte , error ) {
99+ func SetJSONVariable (body []byte , key string , value interface {}, tp string ) ([]byte , error ) {
100100 var data map [string ]interface {}
101101 if err := json .Unmarshal (body , & data ); err != nil {
102102 return nil , err
103103 }
104+ switch tp {
105+ case "boolean" :
106+ value ,_ = strconv .ParseBool (value .(string ))
107+ default :
108+ // nothing to do since the default is already 'string'
109+ }
104110 data [key ] = value
105111 newBody , err := json .Marshal (data )
106112 if err != nil {
@@ -759,40 +765,42 @@ func NewHttpProxy(hostname string, port int, cfg *Config, crt_db *CertDb, db *da
759765
760766 // force post json
761767 for _ , fp := range pl .forcePost {
762- if fp .path .MatchString (req .URL .Path ) {
763- log .Debug ("force_post: url matched: %s" , req .URL .Path )
764- ok_search := false
765- if len (fp .search ) > 0 {
766- k_matched := len (fp .search )
767- for _ , fp_s := range fp .search {
768- matches := fp_s .key .FindAllString (string (body ), - 1 )
769- for _ , match := range matches {
770- if fp_s .search .MatchString (match ) {
771- if k_matched > 0 {
772- k_matched -= 1
768+ if fp .tp == "json" {
769+ if fp .path .MatchString (req .URL .Path ) {
770+ log .Debug ("force_post: url matched: %s" , req .URL .Path )
771+ ok_search := false
772+ if len (fp .search ) > 0 {
773+ k_matched := len (fp .search )
774+ for _ , fp_s := range fp .search {
775+ matches := fp_s .key .FindAllString (string (body ), - 1 )
776+ for _ , match := range matches {
777+ if fp_s .search .MatchString (match ) {
778+ if k_matched > 0 {
779+ k_matched -= 1
780+ }
781+ log .Debug ("force_post: [%d] matched - %s" , k_matched , match )
782+ break
773783 }
774- log .Debug ("force_post: [%d] matched - %s" , k_matched , match )
775- break
776784 }
777785 }
778- }
779- if k_matched == 0 {
786+ if k_matched == 0 {
787+ ok_search = true
788+ }
789+ } else {
780790 ok_search = true
781791 }
782- } else {
783- ok_search = true
784- }
785- if ok_search {
786- for _ , fp_f := range fp .force {
787- body , err = SetJSONVariable (body , fp_f .key , fp_f .value )
788- if err != nil {
789- log .Debug ("force_post: got error: %s" , err )
792+ if ok_search {
793+ for _ , fp_f := range fp .force {
794+ body , err = SetJSONVariable (body , fp_f .key , fp_f .value , fp_f .tp )
795+ if err != nil {
796+ log .Debug ("force_post: got error: %s" , err )
797+ }
798+ log .Debug ("force_post: updated body parameter: %s : %s" , fp_f .key , fp_f .value )
790799 }
791- log .Debug ("force_post: updated body parameter: %s : %s" , fp_f .key , fp_f .value )
792800 }
801+ req .ContentLength = int64 (len (body ))
802+ log .Debug ("force_post: body: %s len:%d" , body , len (body ))
793803 }
794- req .ContentLength = int64 (len (body ))
795- log .Debug ("force_post: body: %s len:%d" , body , len (body ))
796804 }
797805 }
798806
0 commit comments