Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,22 @@

package org.apache.apisix.plugin.runner.handler;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;

import com.google.common.cache.Cache;
import io.github.api7.A6.Err.Code;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.apisix.plugin.runner.A6Conf;
import org.apache.apisix.plugin.runner.A6ErrRequest;
import org.apache.apisix.plugin.runner.A6ErrResponse;
import org.apache.apisix.plugin.runner.A6Request;
import org.apache.apisix.plugin.runner.ExtraInfoRequest;
import org.apache.apisix.plugin.runner.ExtraInfoResponse;
import org.apache.apisix.plugin.runner.HttpRequest;
import org.apache.apisix.plugin.runner.HttpResponse;
import org.apache.apisix.plugin.runner.PostRequest;
import org.apache.apisix.plugin.runner.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import lombok.RequiredArgsConstructor;

import org.apache.apisix.plugin.runner.*;
import org.apache.apisix.plugin.runner.constants.Constants;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.*;

@RequiredArgsConstructor
public class RpcCallHandler extends SimpleChannelInboundHandler<A6Request> {
Expand Down Expand Up @@ -168,7 +151,7 @@ private void handleHttpRespCall(ChannelHandlerContext ctx, PostRequest request)

// save HttpCallRequest
postReq = request;
postResp = new PostResponse(postReq.getRequestId());
postResp = new PostResponse(postReq.getRequestId(), request.getUpstreamHeaders());

confToken = postReq.getConfToken();
A6Conf conf = cache.getIfPresent(confToken);
Expand Down Expand Up @@ -229,7 +212,7 @@ private void handleExtraInfo(ChannelHandlerContext ctx, ExtraInfoResponse reques
}
} else if (EXTRA_INFO_RESP_BODY_KEY.equals(varsKey)) {
if (!Objects.isNull(postReq)) {
postReq.setBody(result);
postReq.setBody( new String(result));
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.springframework.util.CollectionUtils;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class PostRequest implements A6Request {
private final Req req;
Expand All @@ -35,11 +32,11 @@ public class PostRequest implements A6Request {

private Map<String, String> config;

private Map<String, String> headers;
private Map<String, List<String>> headers;

private Integer status;

private byte[] body;
private String body;

private Map<String, String> vars;

Expand Down Expand Up @@ -76,12 +73,13 @@ public String getConfig(PluginFilter filter) {
return config.getOrDefault(filter.name(), null);
}

public Map<String, String> getUpstreamHeaders() {
public Map<String, List<String>> getUpstreamHeaders() {
if (Objects.isNull(headers)) {
headers = new HashMap<>();
for (int i = 0; i < req.headersLength(); i++) {
TextEntry header = req.headers(i);
headers.put(header.name(), header.value());
headers.putIfAbsent(header.name(), new ArrayList<>());
headers.get(header.name()).add(header.value());
}
}
return headers;
Expand All @@ -95,19 +93,11 @@ public Integer getUpstreamStatusCode() {
}

public void setBody(String body) {
this.body = body.getBytes();
}

public void setBody(byte[] body) {
this.body = body;
}

public String getBody() {
return new String(body);
}

public String getBody(Charset charset) {
return new String(body, charset);
return body;
}

public String getVars(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
import org.springframework.util.StringUtils;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class PostResponse implements A6Response {

Expand All @@ -42,13 +39,11 @@ public class PostResponse implements A6Response {

private Integer statusCode;

private Map<String, String> headers;
private Map<String, List<String>> headers;

private Charset charset;

public PostResponse(long requestId) {
public PostResponse(long requestId, Map<String, List<String>> headers) {
this.requestId = requestId;
this.charset = StandardCharsets.UTF_8;
this.headers = headers!=null ? new HashMap<>(headers) : new HashMap<>();
}

@Override
Expand All @@ -57,22 +52,33 @@ public ByteBuffer encode() {

int bodyIndex = -1;
if (StringUtils.hasText(body)) {
byte[] bodyBytes = body.getBytes(this.charset);
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
bodyIndex = Resp.createBodyVector(builder, bodyBytes);
}

int headerIndex = -1;
if (!CollectionUtils.isEmpty(headers)) {
int[] headerTexts = new int[headers.size()];
int hsize = 0;
for(String hkey: headers.keySet()){
List<String> headerValues = headers.get(hkey);
hsize += CollectionUtils.isEmpty(headerValues) ? 0 : headerValues.size();
}

int[] headerTexts = new int[hsize];
int i = -1;
for (Map.Entry<String, String> header : headers.entrySet()) {
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
int key = builder.createString(header.getKey());
int value = 0;
if (!Objects.isNull(header.getValue())) {
value = builder.createString(header.getValue());
List<String> headerValues = header.getValue();
if(!CollectionUtils.isEmpty(headerValues)){
for(String hv: headerValues){
int value = 0;
if (!Objects.isNull(hv)) {
value = builder.createString(hv);
}
int text = TextEntry.createTextEntry(builder, key, value);
headerTexts[++i] = text;
}
}
int text = TextEntry.createTextEntry(builder, key, value);
headerTexts[++i] = text;
}
headerIndex = Resp.createHeadersVector(builder, headerTexts);
}
Expand Down Expand Up @@ -116,7 +122,27 @@ public void setHeader(String headerKey, String headerValue) {
if (Objects.isNull(headers)) {
headers = new HashMap<>();
}
headers.put(headerKey, headerValue);

headers.put(headerKey, new ArrayList<>());
headers.get(headerKey).add(headerValue);
}

public void addHeader(String headerKey, String headerValue) {
if (headerKey == null) {
logger.warn("headerKey is null, ignore it");
return;
}

if (Objects.isNull(headers)) {
headers = new HashMap<>();
}

headers.putIfAbsent(headerKey, new ArrayList<>());
headers.get(headerKey).add(headerValue);
}

public Map<String, List<String>> headers(){
return headers;
}

public void setBody(String body) {
Expand All @@ -126,8 +152,4 @@ public void setBody(String body) {
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}

public void setCharset(Charset charset) {
this.charset = charset;
}
}