diff --git a/src/sig/client.java b/src/sig/client.java index 3b6528b..32a6069 100644 --- a/src/sig/client.java +++ b/src/sig/client.java @@ -46,10 +46,8 @@ public class client { postRes = new POSTRequest("https://postman-echo.com/post","Test body", 30000, "header1","value1", "header2","value2"); System.out.println(((HttpResponse)postRes.run()).body());*/ - POSTRequest postRes = new POSTRequest("https://postman-echo.com/post",Path.of(".gitignore")); + POSTRequest postRes = new POSTRequest("https://postman-echo.com/post",Path.of("..",".gitignore")); System.out.println(((HttpResponse)postRes.run()).body()); - - } catch (FailedResponseException e) { e.printStackTrace(); } diff --git a/src/sig/requests/POSTRequest.java b/src/sig/requests/POSTRequest.java index 5fdaaf9..fc792e7 100644 --- a/src/sig/requests/POSTRequest.java +++ b/src/sig/requests/POSTRequest.java @@ -1,6 +1,8 @@ package sig.requests; +import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.net.URI; import java.net.http.HttpHeaders; import java.net.http.HttpRequest; @@ -8,10 +10,12 @@ import java.net.http.HttpResponse; import java.net.http.HttpClient.Builder; import java.net.http.HttpClient.Version; import java.nio.file.Path; +import java.util.List; import java.util.Optional; import javax.net.ssl.SSLSession; +import sig.MultipartUtility; import sig.exceptions.FailedResponseException; public class POSTRequest extends GETRequest{ @@ -46,40 +50,60 @@ public class POSTRequest extends GETRequest{ @Override public HttpResponse run() throws FailedResponseException { if (uploadFile!=null) { - return new HttpResponse(){ - @Override - public int statusCode() { - return 0; - } - @Override - public HttpRequest request() { - return null; - } - @Override - public Optional> previousResponse() { - return null; - } - @Override - public HttpHeaders headers() { - return null; - } - @Override - public String body() { - return "Response"; - } - @Override - public Optional sslSession() { - return null; - } - @Override - public URI uri() { - return null; - } - @Override - public Version version() { - return null; - } - }; + String charset = "UTF-8"; + File file = uploadFile.toFile(); + String requestURL = url; + + try { + MultipartUtility multipart = new MultipartUtility(requestURL, charset); + + multipart.addHeaderField("User-Agent", "SIG HTTPCLIENT"); + + multipart.addFilePart("fileUpload", file); + + List response = multipart.finish(); + return new HttpResponse(){ + @Override + public int statusCode() { + return 0; + } + @Override + public HttpRequest request() { + return null; + } + @Override + public Optional> previousResponse() { + return null; + } + @Override + public HttpHeaders headers() { + return null; + } + @Override + public String body() { + StringBuilder sb = new StringBuilder(); + for (String s : response) { + sb.append(s); + } + return sb.toString(); + } + @Override + public Optional sslSession() { + return null; + } + @Override + public URI uri() { + return null; + } + @Override + public Version version() { + return null; + } + }; + } catch (IOException ex) { + System.err.println(ex); + throw new FailedResponseException("Could not send response for POST File upload. THIS SHOULD NOT BE HAPPENING!"); + } } else { return super.run(); } diff --git a/utils/search.sh b/utils/search.sh index 5392df5..dd40bcf 100644 --- a/utils/search.sh +++ b/utils/search.sh @@ -37,6 +37,7 @@ function check() { if [ "$g" != "md5" ]; then if [ -f $1$g ]; then + if [ "$g" != ".coauthors" ]; then echo "++Redownload $1$g..." if [ -f "$1$g" ]; then #Read the 2nd line and see if it has a special directory. @@ -58,6 +59,7 @@ function check() { echo "===Could not find directory, assuming regular scripts directory exists." curl -H 'Cache-Control: no-cache, no-store' https://raw.githubusercontent.com/sigonasr2/SigScript/main/$1$g --output scripts/$g fi + fi else echo "++==Downloading $1$g..." curl -H 'Cache-Control: no-cache, no-store' https://raw.githubusercontent.com/sigonasr2/SigScript/main/$1$g --output $1$g @@ -74,4 +76,4 @@ function check() { check $1$g/ fi done -} \ No newline at end of file +}