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

View File

@ -266,14 +266,7 @@ public class ServalDClient implements ServalDHttpConnectionFactory {
// interface ServalDHttpConnectionFactory // interface ServalDHttpConnectionFactory
public HttpURLConnection newServalDHttpConnection(String verb, String path, Iterable<QueryParam> query_params) throws ServalDInterfaceException, IOException public HttpURLConnection newServalDHttpConnection(String verb, String path, Iterable<QueryParam> query_params) throws ServalDInterfaceException, IOException
{ {
StringBuilder str = new StringBuilder(); URL url = new URL("http://127.0.0.1:" + httpPort + path + QueryParam.encode(query_params));
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());
URLConnection uconn = url.openConnection(); URLConnection uconn = url.openConnection();
HttpURLConnection conn; HttpURLConnection conn;
try { 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 { static private void uri_encode_string(StringBuilder str, String text) throws UnsupportedEncodingException {
for (byte b : text.getBytes("UTF-8")) { for (byte b : text.getBytes("UTF-8")) {
if ( (b >= '0' && b <= '9') if ( (b >= '0' && b <= '9')

View File

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