1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

httpuploader now uses new lua uploader

https disabled due to some strange bug with uhttpd
This commit is contained in:
Federico Fissore
2013-05-28 17:01:28 +02:00
parent 765a975414
commit c174737584
2 changed files with 75 additions and 26 deletions

View File

@ -1677,7 +1677,7 @@ public class Sketch {
return false; return false;
} }
Preferences.set(uploader.getAuthorizationKey(), DigestUtils.sha512Hex(dialog.getPassword())); Preferences.set(uploader.getAuthorizationKey(), DigestUtils.sha256Hex(dialog.getPassword()));
} }
success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer); success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);

View File

@ -1,32 +1,32 @@
package processing.app.debug; package processing.app.debug;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.protocol.Protocol;
import processing.app.Base; import processing.app.Base;
import processing.app.Preferences; import processing.app.Preferences;
import processing.app.SerialException; import processing.app.SerialException;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException; import java.net.InetSocketAddress;
import java.io.IOException; import java.net.Socket;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class HttpUploader extends Uploader { public class HttpUploader extends Uploader {
private static final Pattern IPV4_ADDRESS = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})"); private static final Pattern IPV4_ADDRESS = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})");
private static final String PROTOCOL = "http://";
/*
static { static {
Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
} }
*/
private final HttpClient client; private final HttpClient client;
private final String ipAddress; private final String ipAddress;
@ -39,7 +39,7 @@ public class HttpUploader extends Uploader {
throw new IllegalArgumentException(port); throw new IllegalArgumentException(port);
} }
this.ipAddress = matcher.group(1); this.ipAddress = matcher.group(1);
this.baseUrl = "https://" + ipAddress; this.baseUrl = PROTOCOL + ipAddress + "/cgi-bin/luci/arduino";
} }
public boolean requiresAuthorization() { public boolean requiresAuthorization() {
@ -58,9 +58,10 @@ public class HttpUploader extends Uploader {
} }
this.client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); this.client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
String auth = Base64.encodeBase64String(("root:" + Preferences.get(getAuthorizationKey())).getBytes());
int sleptTimes = 1; int sleptTimes = 1;
while (boardNotReady()) { while (boardNotReady(auth)) {
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -72,11 +73,36 @@ public class HttpUploader extends Uploader {
sleptTimes += 1; sleptTimes += 1;
} }
FilePart sketch; StringBuilder uploadRequest = new StringBuilder();
uploadRequest.append(String.format("PWD%1$04d", Preferences.get(getAuthorizationKey()).length())).append(Preferences.get(getAuthorizationKey())).append("\n");
uploadRequest.append("SKETCH\n");
readSketchFile(buildPath, className, uploadRequest);
uploadRequest.append("SKETCH_END\n");
uploadRequest.append("EOF\n");
Socket socket = null;
try { try {
sketch = new FilePart("sketch", new File(buildPath, className + ".hex")); socket = new Socket();
} catch (FileNotFoundException e) { socket.connect(new InetSocketAddress(ipAddress, 9876), 5000);
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
osw.write(uploadRequest.toString());
osw.flush();
BufferedReader isr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String ok = isr.readLine();
if (!"OK".equals(ok)) {
throw new RunnerException("Problem uploading sketch");
}
} catch (IOException e) {
throw new RunnerException(e); throw new RunnerException(e);
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// ignore
}
}
} }
TargetPlatform targetPlatform = Base.getTargetPlatform(); TargetPlatform targetPlatform = Base.getTargetPlatform();
@ -85,12 +111,12 @@ public class HttpUploader extends Uploader {
prefs.putAll(targetPlatform.getTool(prefs.get("upload.tool"))); prefs.putAll(targetPlatform.getTool(prefs.get("upload.tool")));
boolean verbose = prefs.containsKey("upload.verbose") && Boolean.parseBoolean(prefs.get("upload.verbose")); boolean verbose = prefs.containsKey("upload.verbose") && Boolean.parseBoolean(prefs.get("upload.verbose"));
StringPart params = new StringPart("params", verbose ? prefs.get("upload.params.verbose") : prefs.get("upload.params.quiet")); PostMethod post = new PostMethod(baseUrl + "/flash");
post.setRequestHeader("Authorization", "Basic " + auth);
Part[] parts = {sketch, params}; NameValuePair[] data = {
PostMethod post = newPostMethod(); new NameValuePair("params", verbose ? prefs.get("upload.params.verbose") : prefs.get("upload.params.quiet"))
post.setRequestHeader("Cookie", "pwd=" + Preferences.get(getAuthorizationKey())); };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); post.setRequestBody(data);
int statusCode; int statusCode;
try { try {
@ -105,17 +131,40 @@ public class HttpUploader extends Uploader {
} }
} }
protected boolean boardNotReady() { private void readSketchFile(String buildPath, String className, StringBuilder uploadRequest) throws RunnerException {
GetMethod get = new GetMethod(baseUrl + "/ready"); BufferedReader sketchReader = null;
try { try {
return client.executeMethod(get) != HttpStatus.SC_OK; sketchReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(buildPath, className + ".hex"))));
String line;
while ((line = sketchReader.readLine()) != null) {
line = line.trim();
if (line.length() > 0) {
uploadRequest.append(line).append("\n");
}
}
} catch (IOException e) { } catch (IOException e) {
return true; throw new RunnerException(e);
} finally {
if (sketchReader != null) {
try {
sketchReader.close();
} catch (IOException e) {
// ignore
}
}
} }
} }
protected PostMethod newPostMethod() { protected boolean boardNotReady(String auth) {
return new PostMethod(baseUrl + "/upload"); GetMethod get = new GetMethod(baseUrl + "/ready");
get.setRequestHeader("Authorization", "Basic " + auth);
try {
int httpStatus = client.executeMethod(get);
return httpStatus != HttpStatus.SC_OK;
} catch (IOException e) {
e.printStackTrace();
return true;
}
} }
@Override @Override