Search in sources :

Example 1 with PacketLineIn

use of org.eclipse.jgit.transport.PacketLineIn in project gitblit by gitblit.

the class GitDaemonClient method execute.

void execute(final Socket sock) throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
    rawIn = new BufferedInputStream(sock.getInputStream());
    rawOut = new SafeBufferedOutputStream(sock.getOutputStream());
    if (0 < daemon.getTimeout())
        sock.setSoTimeout(daemon.getTimeout() * 1000);
    String cmd = new PacketLineIn(rawIn).readStringRaw();
    final int nul = cmd.indexOf('\0');
    if (nul >= 0) {
        // Newer clients hide a "host" header behind this byte.
        // Currently we don't use it for anything, so we ignore
        // this portion of the command.
        //
        cmd = cmd.substring(0, nul);
    }
    final GitDaemonService srv = getDaemon().matchService(cmd);
    if (srv == null)
        return;
    sock.setSoTimeout(0);
    srv.execute(this, cmd);
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) BufferedInputStream(java.io.BufferedInputStream) SafeBufferedOutputStream(org.eclipse.jgit.util.io.SafeBufferedOutputStream)

Example 2 with PacketLineIn

use of org.eclipse.jgit.transport.PacketLineIn in project gerrit by GerritCodeReview.

the class UploadArchiveIT method zipFormat.

@Test
public void zipFormat() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommit().abbreviate(8).name();
    String c = command(r, abbreviated);
    InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));
    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();
    // Skip length (4 bytes) + 1 byte
    // to position the output stream to the raw zip stream
    byte[] buffer = new byte[5];
    IO.readFully(out, buffer, 0, 5);
    Set<String> entryNames = new TreeSet<>();
    try (ZipArchiveInputStream zip = new ZipArchiveInputStream(out)) {
        ZipArchiveEntry zipEntry = zip.getNextZipEntry();
        while (zipEntry != null) {
            String name = zipEntry.getName();
            entryNames.add(name);
            zipEntry = zip.getNextZipEntry();
        }
    }
    assertThat(entryNames.size()).isEqualTo(1);
    assertThat(Iterables.getOnlyElement(entryNames)).isEqualTo(String.format("%s/%s", abbreviated, PushOneCommit.FILE_NAME));
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TreeSet(java.util.TreeSet) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 3 with PacketLineIn

use of org.eclipse.jgit.transport.PacketLineIn in project gerrit by GerritCodeReview.

the class UploadArchive method readArguments.

/**
   * Read and parse arguments from input stream. This method gets the arguments from input stream,
   * in Pkt-line format, then parses them to fill the options object.
   */
protected void readArguments() throws IOException, Failure {
    String argCmd = "argument ";
    List<String> args = new ArrayList<>();
    // Read arguments in Pkt-Line format
    PacketLineIn packetIn = new PacketLineIn(in);
    for (; ; ) {
        String s = packetIn.readString();
        if (s == PacketLineIn.END) {
            break;
        }
        if (!s.startsWith(argCmd)) {
            throw new Failure(1, "fatal: 'argument' token or flush expected");
        }
        String[] parts = s.substring(argCmd.length()).split("=", 2);
        for (String p : parts) {
            args.add(p);
        }
    }
    try {
        // Parse them into the 'options' field
        CmdLineParser parser = new CmdLineParser(options);
        parser.parseArgument(args);
        if (options.path == null || Arrays.asList(".").equals(options.path)) {
            options.path = Collections.emptyList();
        }
    } catch (CmdLineException e) {
        throw new Failure(2, "fatal: unable to parse arguments, " + e);
    }
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) CmdLineParser(org.kohsuke.args4j.CmdLineParser) ArrayList(java.util.ArrayList) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 4 with PacketLineIn

use of org.eclipse.jgit.transport.PacketLineIn in project gerrit by GerritCodeReview.

the class UploadArchiveIT method archiveNotPermitted.

private void archiveNotPermitted() throws Exception {
    PushOneCommit.Result r = createChange();
    String abbreviated = r.getCommit().abbreviate(8).name();
    String c = command(r, abbreviated);
    InputStream out = adminSshSession.exec2("git-upload-archive " + project.get(), argumentsToInputStream(c));
    // Wrap with PacketLineIn to read ACK bytes from output stream
    PacketLineIn in = new PacketLineIn(out);
    String tmp = in.readString();
    assertThat(tmp).isEqualTo("ACK");
    tmp = in.readString();
    tmp = in.readString();
    tmp = tmp.substring(1);
    assertThat(tmp).isEqualTo("fatal: upload-archive not permitted");
}
Also used : PacketLineIn(org.eclipse.jgit.transport.PacketLineIn) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Aggregations

PacketLineIn (org.eclipse.jgit.transport.PacketLineIn)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 BufferedInputStream (java.io.BufferedInputStream)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 SafeBufferedOutputStream (org.eclipse.jgit.util.io.SafeBufferedOutputStream)1 Test (org.junit.Test)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1 CmdLineParser (org.kohsuke.args4j.CmdLineParser)1