Skip to content

Commit

Permalink
[FIX] RESTXQ: accept all if Accept header is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGruen committed Feb 25, 2015
1 parent bd2a5e3 commit be183aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion basex-api/src/main/java/org/basex/BaseXHTTP.java
Expand Up @@ -376,7 +376,7 @@ private static void stop(final int port) throws IOException {
private static boolean ping(final String host, final int port, final boolean ssl) {
try {
// create connection
new URL((ssl ? "https://" : "http://") + host + ':' + port).openConnection().getInputStream();
new IOUrl((ssl ? "https://" : "http://") + host + ':' + port).connection().getInputStream();
return true;
} catch(final FileNotFoundException | SSLHandshakeException ex) {
// if page is not found, server is running
Expand Down
28 changes: 16 additions & 12 deletions basex-api/src/main/java/org/basex/http/HTTPContext.java
Expand Up @@ -222,21 +222,25 @@ public String db() {
*/
public HTTPAccept[] accepts() {
final String accept = req.getHeader(ACCEPT);
if(accept == null) return new HTTPAccept[0];

final ArrayList<HTTPAccept> list = new ArrayList<>();
for(final String produce : accept.split("\\s*,\\s*")) {
if(accept == null) {
final HTTPAccept acc = new HTTPAccept();
// check if quality factor was specified
final Matcher m = QF.matcher(produce);
if(m.find()) {
acc.type = m.group(1);
acc.qf = toDouble(token(m.group(2)));
} else {
acc.type = produce;
acc.type = "*/*";
list.add(acc);
} else {
for(final String produce : accept.split("\\s*,\\s*")) {
final HTTPAccept acc = new HTTPAccept();
// check if quality factor was specified
final Matcher m = QF.matcher(produce);
if(m.find()) {
acc.type = m.group(1);
acc.qf = toDouble(token(m.group(2)));
} else {
acc.type = produce;
}
// only accept valid double values
if(acc.qf > 0 && acc.qf <= 1) list.add(acc);
}
// only accept valid double values
if(acc.qf > 0 && acc.qf <= 1) list.add(acc);
}
return list.toArray(new HTTPAccept[list.size()]);
}
Expand Down
18 changes: 8 additions & 10 deletions basex-api/src/test/java/org/basex/http/HTTPTest.java
Expand Up @@ -11,7 +11,7 @@

import org.basex.*;
import org.basex.core.*;
import org.basex.core.StaticOptions.*;
import org.basex.core.StaticOptions.AuthMethod;
import org.basex.io.*;
import org.basex.io.in.*;
import org.basex.io.out.*;
Expand Down Expand Up @@ -143,11 +143,9 @@ private static String request(final String query, final HttpMethod method)
* @return string result, or {@code null} for a failure.
* @throws IOException I/O exception
*/
protected static String request(final String query, final String method)
throws IOException {

final URL url = new URL(root + query);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
protected static String request(final String query, final String method) throws IOException {
final IOUrl url = new IOUrl(root + query);
final HttpURLConnection conn = (HttpURLConnection) url.connection();
try {
conn.setRequestMethod(method);
return normNL(read(new BufferInput(conn.getInputStream())));
Expand All @@ -170,8 +168,8 @@ protected static String post(final String query, final String request, final Str
throws IOException {

// create connection
final URL url = new URL(root + query);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
final IOUrl url = new IOUrl(root + query);
final HttpURLConnection conn = (HttpURLConnection) url.connection();
conn.setDoOutput(true);
conn.setRequestMethod(POST.name());
conn.setRequestProperty(HttpText.CONTENT_TYPE, type);
Expand Down Expand Up @@ -242,8 +240,8 @@ protected static void put(final String u, final InputStream is) throws IOExcepti
protected static void put(final String u, final InputStream is, final String ctype)
throws IOException {

final URL url = new URL(root + u);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
final IOUrl url = new IOUrl(root + u);
final HttpURLConnection conn = (HttpURLConnection) url.connection();
conn.setDoOutput(true);
conn.setRequestMethod(PUT.name());
if(ctype != null) conn.setRequestProperty(HttpText.CONTENT_TYPE, ctype);
Expand Down
5 changes: 3 additions & 2 deletions basex-api/src/test/java/org/basex/http/rest/RESTTest.java
Expand Up @@ -6,6 +6,7 @@
import java.net.*;

import org.basex.http.*;
import org.basex.io.*;
import org.junit.*;

/**
Expand Down Expand Up @@ -57,8 +58,8 @@ protected static void assertContains(final String str, final String sub) {
* @throws IOException I/O exception
*/
protected static String contentType(final String query) throws IOException {
final URL url = new URL(REST_ROOT + query);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
final IOUrl url = new IOUrl(REST_ROOT + query);
final HttpURLConnection conn = (HttpURLConnection) url.connection();
try {
read(conn.getInputStream());
return conn.getContentType();
Expand Down

0 comments on commit be183aa

Please sign in to comment.