You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/12/13 03:57:47 UTC

[GitHub] [apisix-go-plugin-runner] GhangZh edited a comment on issue #53: request help: support http proxy

GhangZh edited a comment on issue #53:
URL: https://github.com/apache/apisix-go-plugin-runner/issues/53#issuecomment-992085667


   I wrote a demo, but I found that the response body is empty ,For example, if I add this plugin to a.com, and I request a.com, the plugin will request b.com and do the processing, now the processing is successful but there is no response content from a.com.
   ```bash
   package plugins
   
   import (
   	"encoding/json"
   	"fmt"
   	pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
   	"github.com/apache/apisix-go-plugin-runner/pkg/log"
   	"github.com/apache/apisix-go-plugin-runner/pkg/plugin"
   	"github.com/sirupsen/logrus"
   	"net/http"
   	"time"
   )
   
   func init() {
   	err := plugin.RegisterPlugin(&ForwardAuth{})
   	if err != nil {
   		log.Fatalf("failed to register plugin ForwardAuth: %s", err)
   	}
   }
   
   // ForwardAuth is a demo to show how to return data directly instead of proxying
   // it to the upstream.
   type ForwardAuth struct {
   }
   
   type ForwardAuthConf struct {
   	Body string `json:"body"`
   }
   
   func (p *ForwardAuth) Name() string {
   	return "forward-auth"
   }
   
   func (p *ForwardAuth) ParseConf(in []byte) (interface{}, error) {
   	conf := ForwardAuthConf{}
   	err := json.Unmarshal(in, &conf)
   	return conf, err
   }
   func (p *ForwardAuth) Filter(conf interface{}, w http.ResponseWriter, r pkgHTTP.Request) {
   	req, err := http.NewRequest("GET", "http://xxx", nil)
   	if err != nil {
   		logrus.Errorf("send request failed err:%v", err)
   		return
   	}
   	//req.Header.Set("Cookie", r.Header().Get("Cookie"))
   	client := http.Client{
   		Timeout: 5 * time.Second,
   	}
   	resp, err := client.Do(req)
   	if err != nil {
   		logrus.Errorf("get response failed err:%v", err)
   		return
   	}
   	if resp == nil {
   		logrus.Errorf("response is nil")
   		return
   	}
   	defer resp.Body.Close()
   
   	if len(resp.Header) > 0 {
   		for k, v := range resp.Header {
   			if v != nil {
   				w.Header().Set(k, v[0])
   			}
   		}
   	}
   
   	w.Header().Add("X-Resp-A6-Runner", "Go")
   	return
   
   }
   ```
   not use plugin
   ![image](https://user-images.githubusercontent.com/92301646/145749863-5dfe9dd4-4e2a-483c-a86c-deff3d2bca86.png)
   after use pulgin
   ![image](https://user-images.githubusercontent.com/92301646/145750006-b43fc78c-31c5-4cc4-a8e8-eedc04efb54a.png)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org