Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 54
Fix/adfa 1894#841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Fix/adfa 1894 #841
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ed8c32
Update WebServer.kt to add /pr endpoint for the RecentProjects database
davidschachterADFA e091b40
Update WebServer.kt--change sort column for /db endpoint from rowid t…
davidschachterADFA e95ab3b
Update WebServer.kt -- Add /pr endpoint to serve RecentProjects data …
davidschachterADFA fa2ef90
Update WebServer.kt-address CodeRabbit comments about resource leaks
davidschachterADFA df15ea9
Merge branch 'stage' into fix/ADFA-1894
hal-eisen-adfa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
176 changes: 131 additions & 45 deletions
176 app/src/main/java/com/itsaky/androidide/localWebServer/WebServer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,7 +21,9 @@ data class ServerConfig( | ||
| val debugDatabasePath: String = android.os.Environment.getExternalStorageDirectory().toString() + | ||
| "/Download/documentation.db", | ||
| val debugEnablePath: String = android.os.Environment.getExternalStorageDirectory().toString() + | ||
| "/Download/CodeOnTheGo.webserver.debug" | ||
| "/Download/CodeOnTheGo.webserver.debug", | ||
| // Yes, this is hack code. | ||
| val projectDatabasePath: String = "/data/data/com.itsaky.androidide/databases/RecentProject_database" | ||
| ) | ||
| class WebServer(private val config: ServerConfig) { | ||
| @@ -175,6 +177,8 @@ FROM LastChange | ||
| // Handle the special "/db" endpoint with highest priority | ||
| if (path == "db") { | ||
| return handleDbEndpoint(writer, output) | ||
| } else if (path == "pr") { | ||
| return handlePrEndpoint(writer, output) | ||
| } | ||
| val query = """ | ||
| @@ -278,70 +282,152 @@ WHERE path = ? | ||
| // Build the SELECT query for the 20 most recent rows | ||
| val selectColumns = columnNames.joinToString(", ") | ||
| val dataQuery = "SELECT $selectColumns FROM LastChange ORDER BY rowid DESC LIMIT 20" | ||
| val dataQuery = "SELECT $selectColumns FROM LastChange ORDER BY changeTime DESC LIMIT 20" | ||
hal-eisen-adfa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val dataCursor = database.rawQuery(dataQuery, arrayOf()) | ||
| val rowCount = dataCursor.count | ||
| if (debugEnabled) log.debug("Retrieved {} rows from LastChange table", rowCount) | ||
| // Generate HTML table | ||
| val html = buildString { | ||
| appendLine("<!DOCTYPE html>") | ||
| appendLine("<html>") | ||
| appendLine("<head>") | ||
| appendLine("<title>LastChange Table</title>") | ||
| appendLine("<style>") | ||
| appendLine("table { border-collapse: collapse; width: 100%; }") | ||
| appendLine("th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }") | ||
| appendLine("th { background-color: #f2f2f2; }") | ||
| appendLine("</style>") | ||
| appendLine("</head>") | ||
| appendLine("<body>") | ||
| appendLine("<h1>LastChange Table (20 Most Recent Rows)</h1>") | ||
| appendLine("<table width='100%'>") | ||
| var html = getTableHtml("LastChange Table", "LastChange Table (20 Most Recent Rows)") | ||
| // Add header row | ||
| appendLine("<tr>") | ||
| for (columnName in columnNames) { | ||
| appendLine("<th>${escapeHtml(columnName)}</th>") | ||
| } | ||
| appendLine("</tr>") | ||
| // Add header row | ||
| html += """<tr>""" | ||
| for (columnName in columnNames) { | ||
| html += """<th>${escapeHtml(columnName)}</th>""" | ||
| } | ||
| html += """</tr>""" | ||
| // Add data rows | ||
| while (dataCursor.moveToNext()) { | ||
| appendLine("<tr>") | ||
| for (i in 0 until columnCount) { | ||
| val value = dataCursor.getString(i) ?: "" | ||
| appendLine("<td>${escapeHtml(value)}</td>") | ||
| } | ||
| appendLine("</tr>") | ||
| // Add data rows | ||
| while (dataCursor.moveToNext()) { | ||
| html += """<tr>""" | ||
| for (i in 0 until columnCount) { | ||
| html += """<td>${escapeHtml(dataCursor.getString(i) ?: "")}</td>""" | ||
| } | ||
| appendLine("</table>") | ||
| appendLine("</body>") | ||
| appendLine("</html>") | ||
| html += """</tr>""" | ||
| } | ||
| html += """</table></body></html>""" | ||
| dataCursor.close() | ||
| // Send the response | ||
| val htmlBytes = html.toByteArray(Charsets.UTF_8) | ||
| writer.println("HTTP/1.1 200 OK") | ||
| writer.println("Content-Type: text/html; charset=utf-8") | ||
| writer.println("Content-Length: ${htmlBytes.size}") | ||
| writer.println("Connection: close") | ||
| writer.println() | ||
| writer.flush() | ||
| output.write(htmlBytes) | ||
| output.flush() | ||
| writeNormalToClient(writer, output, html) | ||
| } catch (e: Exception) { | ||
| log.error("Error handling /db endpoint: {}", e.message) | ||
| sendError(writer, 500, "Internal Server Error", "Error generating database table: ${e.message}") | ||
| } | ||
| } | ||
| private fun handlePrEndpoint(writer: PrintWriter, output: java.io.OutputStream) { | ||
| var projectDatabase : SQLiteDatabase? = null | ||
| try { | ||
| projectDatabase = SQLiteDatabase.openDatabase(config.projectDatabasePath, | ||
| null, | ||
| SQLiteDatabase.OPEN_READONLY) | ||
| if (projectDatabase == null) { | ||
| log.error("Error handling /pr endpoint 2. Could not open ${config.projectDatabasePath}.") | ||
| sendError(writer, 500, "Internal Server Error", "Error accessing database 2") | ||
| } else { | ||
| realHandlePrEndpoint(writer, output, projectDatabase) | ||
| } | ||
| } catch (e: Exception) { | ||
| log.error("Error handling /pr endpoint: {}", e.message) | ||
| sendError(writer, 500, "Internal Server Error", "Error generating database table: ${e.message}") | ||
| } finally { | ||
| if (projectDatabase != null) { | ||
| projectDatabase.close() | ||
| } | ||
| } | ||
| } | ||
| private fun realHandlePrEndpoint(writer: PrintWriter, output: java.io.OutputStream, projectDatabase: SQLiteDatabase) { | ||
| val query = """ | ||
| SELECT id, | ||
| name, | ||
| DATETIME(create_at / 1000, 'unixepoch'), | ||
| DATETIME(last_modified / 1000, 'unixepoch'), | ||
| location, | ||
| template_name, | ||
| language | ||
| FROM recent_project_table | ||
| ORDER BY last_modified DESC""" | ||
| val cursor = projectDatabase.rawQuery(query, arrayOf()) | ||
| if (debugEnabled) log.debug("Retrieved {} rows.", cursor.getCount()) | ||
| var html = getTableHtml("Projects", "Projects") + """ | ||
| <tr> | ||
| <th>Id</th> | ||
| <th>Name</th> | ||
| <th>Created</th> | ||
| <th>Modified <span style="font-family: sans-serif">V</span></th> | ||
| <th>Directory</th> | ||
| <th>Template</th> | ||
| <th>Language</th> | ||
| </tr>""" | ||
| while (cursor.moveToNext()) { | ||
| html += """<tr> | ||
| <td>${escapeHtml(cursor.getString(0) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(1) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(2) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(3) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(4) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(5) ?: "")}</td> | ||
| <td>${escapeHtml(cursor.getString(6) ?: "")}</td> | ||
| </tr>""" | ||
| } | ||
| html += "</table></body></html>" | ||
| cursor.close() | ||
| writeNormalToClient(writer, output, html) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Get HTML for table response page. | ||
| */ | ||
| private fun getTableHtml(title: String, tableName: String): String { | ||
| return """<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>${escapeHtml(title)}</title> | ||
| <style> | ||
| table { border-collapse: collapse; width: 100%; } | ||
| th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } | ||
| th { background-color: #f2f2f2; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h1>${escapeHtml(tableName)}</h1> | ||
| <table width='100%'>""" | ||
| } | ||
| /** | ||
| * Tail of writing table data back to client. | ||
| */ | ||
| private fun writeNormalToClient(writer: PrintWriter, output: java.io.OutputStream, html: String) { | ||
| val htmlBytes = html.toByteArray(Charsets.UTF_8) | ||
| writer.println("HTTP/1.1 200 OK") | ||
| writer.println("Content-Type: text/html; charset=utf-8") | ||
| writer.println("Content-Length: ${htmlBytes.size}") | ||
| writer.println("Connection: close") | ||
| writer.println() | ||
| writer.flush() | ||
| output.write(htmlBytes) | ||
| output.flush() | ||
| } | ||
| /** | ||
| * Escapes HTML special characters to prevent XSS attacks. | ||
| * Converts <, >, &, ", and ' to their HTML entity equivalents. | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.