2
0

LuaHTTPS.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package org.love2d.luahttps;
  2. import android.text.TextUtils;
  3. import android.util.Log;
  4. import androidx.annotation.Keep;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.ProtocolException;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. @Keep
  18. class LuaHTTPS {
  19. static private String TAG = "LuaHTTPS";
  20. private String urlString;
  21. private String method;
  22. private byte[] postData;
  23. private byte[] response;
  24. private int responseCode;
  25. private HashMap<String, String> headers;
  26. public LuaHTTPS() {
  27. headers = new HashMap<String, String>();
  28. reset();
  29. }
  30. public void reset() {
  31. urlString = null;
  32. method = "GET";
  33. postData = null;
  34. response = null;
  35. responseCode = 0;
  36. headers.clear();
  37. }
  38. @Keep
  39. public void setUrl(String url) {
  40. urlString = url;
  41. }
  42. @Keep
  43. public void setPostData(byte[] postData) {
  44. this.postData = postData;
  45. }
  46. @Keep
  47. public void setMethod(String method) {
  48. this.method = method.toUpperCase();
  49. }
  50. @Keep
  51. public void addHeader(String key, String value) {
  52. headers.put(key, value);
  53. }
  54. @Keep
  55. public String[] getInterleavedHeaders() {
  56. ArrayList<String> resultInterleaved = new ArrayList<String>();
  57. for (Map.Entry<String, String> header: headers.entrySet()) {
  58. String key = header.getKey();
  59. String value = header.getValue();
  60. if (key != null && value != null) {
  61. resultInterleaved.add(key);
  62. resultInterleaved.add(value);
  63. }
  64. }
  65. String[] result = new String[resultInterleaved.size()];
  66. resultInterleaved.toArray(result);
  67. return result;
  68. }
  69. @Keep
  70. public int getResponseCode() {
  71. return responseCode;
  72. }
  73. @Keep
  74. public byte[] getResponse() {
  75. return response;
  76. }
  77. @Keep
  78. public boolean request() {
  79. if (urlString == null) {
  80. return false;
  81. }
  82. URL url;
  83. try {
  84. url = new URL(urlString);
  85. if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https")) {
  86. return false;
  87. }
  88. } catch (MalformedURLException e) {
  89. Log.e(TAG, "Error", e);
  90. return false;
  91. }
  92. HttpURLConnection connection;
  93. try {
  94. connection = (HttpURLConnection) url.openConnection();
  95. } catch (IOException e) {
  96. Log.e(TAG, "Error", e);
  97. return false;
  98. }
  99. // Set request method
  100. try {
  101. connection.setRequestMethod(method);
  102. } catch (ProtocolException e) {
  103. Log.e(TAG, "Error", e);
  104. return false;
  105. }
  106. // Set header
  107. for (Map.Entry<String, String> headerData: headers.entrySet()) {
  108. connection.setRequestProperty(headerData.getKey(), headerData.getValue());
  109. }
  110. // Set post data
  111. if (postData != null && canSendData()) {
  112. connection.setDoOutput(true);
  113. connection.setChunkedStreamingMode(0);
  114. try {
  115. OutputStream out = connection.getOutputStream();
  116. out.write(postData);
  117. } catch (Exception e) {
  118. Log.e(TAG, "Error", e);
  119. connection.disconnect();
  120. return false;
  121. }
  122. }
  123. // Request
  124. try {
  125. InputStream in;
  126. // Set response code
  127. responseCode = connection.getResponseCode();
  128. if (responseCode >= 400) {
  129. in = connection.getErrorStream();
  130. } else {
  131. in = connection.getInputStream();
  132. }
  133. // Read response
  134. int readed;
  135. byte[] temp = new byte[4096];
  136. ByteArrayOutputStream response = new ByteArrayOutputStream();
  137. while ((readed = in.read(temp)) != -1) {
  138. response.write(temp, 0, readed);
  139. }
  140. this.response = response.toByteArray();
  141. response.close();
  142. // Read headers
  143. headers.clear();
  144. for (Map.Entry<String, List<String>> header: connection.getHeaderFields().entrySet()) {
  145. headers.put(header.getKey(), TextUtils.join(", ", header.getValue()));
  146. }
  147. } catch (Exception e) {
  148. Log.e(TAG, "Error", e);
  149. connection.disconnect();
  150. return false;
  151. }
  152. connection.disconnect();
  153. return true;
  154. }
  155. private boolean canSendData() {
  156. return !method.equals("GET") && !method.equals("HEAD");
  157. }
  158. }