Correctly encode rhizome list query parameters

This commit is contained in:
Jeremy Lakeman 2018-05-28 17:18:21 +09:30
parent 9522575417
commit 659d726687
4 changed files with 32 additions and 26 deletions

View File

@ -26,6 +26,7 @@ import org.servalproject.json.JSONTableScanner;
import org.servalproject.json.JSONTokeniser;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
@ -43,6 +44,10 @@ public abstract class AbstractJsonList<T, E extends Exception> {
String verb;
String url;
public Request(String verb, String url, Iterable<ServalDHttpConnectionFactory.QueryParam> parms) throws UnsupportedEncodingException {
this(verb, url + ServalDHttpConnectionFactory.QueryParam.encode(parms));
}
public Request(String verb, String url) {
this.verb = verb;
this.url = url;
@ -54,7 +59,7 @@ public abstract class AbstractJsonList<T, E extends Exception> {
this.table = table;
}
protected abstract Request getRequest();
protected abstract Request getRequest() throws UnsupportedEncodingException;
public boolean isConnected(){
return this.json != null;

View File

@ -266,14 +266,7 @@ public class ServalDClient implements ServalDHttpConnectionFactory {
// interface ServalDHttpConnectionFactory
public HttpURLConnection newServalDHttpConnection(String verb, String path, Iterable<QueryParam> query_params) throws ServalDInterfaceException, IOException
{
StringBuilder str = new StringBuilder();
char sep = '?';
for (QueryParam param : query_params) {
str.append(sep);
param.uri_encode(str);
sep = '&';
}
URL url = new URL("http://127.0.0.1:" + httpPort + path + str.toString());
URL url = new URL("http://127.0.0.1:" + httpPort + path + QueryParam.encode(query_params));
URLConnection uconn = url.openConnection();
HttpURLConnection conn;
try {

View File

@ -45,6 +45,17 @@ public interface ServalDHttpConnectionFactory {
}
}
public static String encode(Iterable<QueryParam> query_params) throws UnsupportedEncodingException {
StringBuilder str = new StringBuilder();
char sep = '?';
for (QueryParam param : query_params) {
str.append(sep);
param.uri_encode(str);
sep = '&';
}
return str.toString();
}
static private void uri_encode_string(StringBuilder str, String text) throws UnsupportedEncodingException {
for (byte b : text.getBytes("UTF-8")) {
if ( (b >= '0' && b <= '9')

View File

@ -31,7 +31,9 @@ import org.servalproject.servaldna.ServalDInterfaceException;
import org.servalproject.servaldna.SubscriberId;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Vector;
public class RhizomeBundleList extends AbstractJsonList<RhizomeListBundle, IOException> {
@ -80,7 +82,7 @@ public class RhizomeBundleList extends AbstractJsonList<RhizomeListBundle, IOExc
}
@Override
protected Request getRequest() {
protected Request getRequest() throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (this.sinceToken == null)
sb.append("/restful/rhizome/bundlelist.json");
@ -89,22 +91,17 @@ public class RhizomeBundleList extends AbstractJsonList<RhizomeListBundle, IOExc
else
sb.append("/restful/rhizome/newsince/").append(this.sinceToken).append("/bundlelist.json");
char parmDelim = '?';
if (service != null){
sb.append(parmDelim).append("service=").append(service);
parmDelim='&';
}
if (name!=null) {
sb.append(parmDelim).append("name=").append(name);
parmDelim='&';
}
if (sender!=null) {
sb.append(parmDelim).append("sender=").append(sender.toHex());
parmDelim='&';
}
if (recipient!=null)
sb.append(parmDelim).append("recipient=").append(recipient.toHex());
return new Request("GET", sb.toString());
Vector<ServalDHttpConnectionFactory.QueryParam> query_params = new Vector<ServalDHttpConnectionFactory.QueryParam>();
if (service != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("service", service));
if (name != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("name", name));
if (sender != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("sender", sender.toHex()));
if (recipient != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("recipient", recipient.toHex()));
return new Request("GET", sb.toString(), query_params);
}
@Override