Remove implementation specific Java hack and allow POST verb for keyring set command

This commit is contained in:
Jeremy Lakeman 2018-03-28 15:24:57 +10:30
parent 4f5567e2c1
commit 65d0e3a47c
3 changed files with 2 additions and 26 deletions

View File

@ -286,28 +286,4 @@ public class ServalDClient implements ServalDHttpConnectionFactory {
return conn;
}
/* A hack to force java.net.HttpURLConnection to accept the non-standard "PATCH" request method.
* Taken from https://stackoverflow.com/a/46323891
*/
private static class HttpURLConnectionHack {
public HttpURLConnectionHack() {
try {
Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
methodsField.setAccessible(true);
String[] oldMethods = (String[]) methodsField.get(null);
Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
methodsSet.add("PATCH");
String[] newMethods = methodsSet.toArray(new String[0]);
methodsField.set(null, newMethods);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
private static final HttpURLConnectionHack dummy = new HttpURLConnectionHack();
}

View File

@ -195,7 +195,7 @@ public class KeyringCommon
query_params.add(new ServalDHttpConnectionFactory.QueryParam("name", name));
if (pin != null)
query_params.add(new ServalDHttpConnectionFactory.QueryParam("pin", pin));
HttpURLConnection conn = connector.newServalDHttpConnection("PATCH", "/restful/keyring/" + sid.toHex(), query_params);
HttpURLConnection conn = connector.newServalDHttpConnection("POST", "/restful/keyring/" + sid.toHex(), query_params);
conn.connect();
Status status = receiveRestfulResponse(conn, HttpURLConnection.HTTP_OK);
try {

View File

@ -53,7 +53,7 @@ static int restful_keyring_(httpd_request *r, const char *remainder)
if (strcmp(end, "") == 0) {
if (r->http.verb == HTTP_VERB_GET)
return restful_keyring_get(r, "");
if (r->http.verb == HTTP_VERB_PATCH)
if (r->http.verb == HTTP_VERB_PATCH || r->http.verb == HTTP_VERB_POST)
return restful_keyring_set(r, "");
if (r->http.verb == HTTP_VERB_DELETE)
return restful_keyring_remove(r, "");