/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4255280
* @summary URL.getContent() loses first six bytes for ftp URLs
*/
public class FtpGetContent {
/**
* A class that simulates, on a separate, an FTP server.
*/
private int port;
private boolean done = false;
private boolean portEnabled = true;
private boolean pasvEnabled = true;
private boolean list = false;
/**
* This Inner class will handle ONE client at a time.
* That's where 99% of the protocol handling is done.
*/
"NOOP", "RETR", "PASV", "PORT", "LIST", "REIN",
"QUIT", "STOR", "NLST", "RNFR", "RNTO" };
/**
* Parses a line to match it with one of the supported FTP commands.
* Returns the command number.
*/
return ERROR;
if (blank < 0)
if (blank < 3)
return ERROR;
else
if (s.equalsIgnoreCase(cmds[i]))
return i+1;
}
return ERROR;
}
}
protected boolean isPasvSet() {
try {
} catch (IOException ex) {
}
}
return true;
return false;
}
/**
* Open the data socket with the client. This can be the
* result of a "PASV" or "PORT" command.
*/
try {
if (isPasvSet()) {
return s.getOutputStream();
}
data_port = 0;
return s.getOutputStream();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
try {
if (isPasvSet()) {
return s.getInputStream();
}
data_port = 0;
return s.getInputStream();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Handles the protocol exchange with the client.
*/
public void run() {
boolean done = false;
int res;
boolean logged = false;
boolean waitpass = false;
try {
return;
}
while (!done) {
try {
continue;
}
switch (res) {
case ERROR:
break;
case USER:
logged = true;
} else {
waitpass = true;
}
} else {
}
break;
case PASS:
logged = true;
waitpass = false;
} else
break;
case QUIT:
done = true;
break;
case TYPE:
break;
case CWD:
else
break;
case CDUP:
break;
case PWD:
break;
case PASV:
if (!pasvEnabled) {
continue;
}
try {
} catch (IOException ssex) {
}
break;
case PORT:
if (!portEnabled) {
continue;
}
int i=0, j=4;
while (j>0) {
if (i < 0)
break;
j--;
}
if (j != 0) {
continue;
}
try {
data_port = 0;
}
break;
case RETR:
{
for (int x = 0; x < filesize ; x++)
list = false;
} else
}
break;
case NLST:
case LIST:
{
list = true;
} else
}
break;
case STOR:
{
int val;
do {
} while (val != -1);
} else
}
break;
}
} catch (IOException ioe) {
try {
}
done = true;
}
}
}
}
try {
} catch (IOException e) {
}
}
public FtpServer() {
this(21);
}
public int getPort() {
return server.getLocalPort();
return 0;
}
/**
* A way to tell the server that it can stop.
*/
synchronized public void terminate() {
done = true;
}
synchronized boolean done() {
return done;
}
portEnabled = ok;
}
pasvEnabled = ok;
}
return username;
}
return password;
}
return cwd;
}
return filename;
}
return type;
}
boolean getList() {
return list;
}
/*
* All we got to do here is create a ServerSocket and wait for connections.
* When a connection happens, we just have to create a thread that will
* handle it.
*/
public void run() {
try {
while (!done()) {
}
} catch(Exception e) {
} finally {
}
}
}
}
try {
// Now let's check the URL handler
byte[] buffer = new byte[1024];
int totalBytes = 0;
while (bytesRead != -1) {
totalBytes += bytesRead;
}
if (totalBytes != filesize)
throw new RuntimeException("wrong file size!");
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
} finally {
}
}
}