Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the name of the gremlin request less than 256 bytes #313

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

package com.baidu.hugegraph.api.job;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.HashMap;
import java.util.Map;

@@ -32,6 +37,7 @@

import org.slf4j.Logger;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.api.filter.StatusFilter.Status;
@@ -58,6 +64,8 @@ public class GremlinAPI extends API {

private static final Logger LOG = Log.logger(RestServer.class);

private static final int MAX_NAME_LENGTH = 256;

private static final Histogram gremlinJobInputHistogram =
MetricsUtil.registerHistogram(GremlinAPI.class, "gremlin-input");

@@ -148,7 +156,27 @@ public Map<String, String> aliases() {

public String name() {
// Get the first line of script as the name
return this.gremlin.split("\r\n|\r|\n", 2)[0];
String firstLine = this.gremlin.split("\r\n|\r|\n", 2)[0];
final Charset charset = Charset.forName(CHARSET);
final byte[] bytes = firstLine.getBytes(charset);
if (bytes.length <= MAX_NAME_LENGTH) {
return firstLine;
}

/*
* Reference https://stackoverflow.com/questions/3576754/truncating-strings-by-bytes
*/
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.reset();

ByteBuffer buffer = ByteBuffer.wrap(bytes, 0, MAX_NAME_LENGTH);
try {
return decoder.decode(buffer).toString();
} catch (CharacterCodingException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just catch line 175

throw new HugeException("Failed to decode truncated bytes of " +
"gremlin first line", e);
}
}

@Override