Mercurial
comparison postdog/main.c @ 54:b3e82d22f961
[PostDog] Initial commit BROKEN
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 19 Dec 2025 13:58:52 -0800 |
| parents | |
| children | d4cdb87212fb |
comparison
equal
deleted
inserted
replaced
| 53:82d1fe4d4ee6 | 54:b3e82d22f961 |
|---|---|
| 1 /** | |
| 2 * Entirely written by Claude AI. | |
| 3 */ | |
| 4 #include <stdio.h> | |
| 5 #include <stdlib.h> | |
| 6 #include <string.h> | |
| 7 #include <time.h> | |
| 8 #include <sys/stat.h> | |
| 9 #include <dirent.h> | |
| 10 | |
| 11 // third party | |
| 12 #include <curl/curl.h> | |
| 13 #include "third_party/raylib/include/raylib.h" | |
| 14 #define RAYGUI_IMPLEMENTATION | |
| 15 #include "third_party/raylib/include/raygui.h" | |
| 16 | |
| 17 #define SCREEN_WIDTH 1280 | |
| 18 #define SCREEN_HEIGHT 780 | |
| 19 #define SIDEBAR_WIDTH 200 | |
| 20 #define URL_INPUT_HEIGHT 40 | |
| 21 #define PADDING 15 | |
| 22 | |
| 23 #define TEXT_SIZE 10 | |
| 24 #define LINE_HEIGHT 15 | |
| 25 | |
| 26 #define METHOD_BUTTON_WIDTH 100 | |
| 27 #define METHOD_BUTTON_HEIGHT 40 | |
| 28 #define SEND_BUTTON_WIDTH 100 | |
| 29 #define SEND_BUTTON_HEIGHT 40 | |
| 30 | |
| 31 #define JSON_INPUT_BUFFER_LEN 8192 | |
| 32 #define HEADER_INPUT_BUFFER_LEN 4096 | |
| 33 #define PARAM_INPUT_BUFFER_LEN 4096 | |
| 34 #define MAX_HISTORY_ITEMS 100 | |
| 35 | |
| 36 // Structure to hold response data | |
| 37 typedef struct { | |
| 38 char *data; | |
| 39 size_t size; | |
| 40 } ResponseBuffer; | |
| 41 | |
| 42 // Structure to hold history item | |
| 43 typedef struct { | |
| 44 char filename[256]; | |
| 45 char displayName[128]; | |
| 46 char method[16]; | |
| 47 time_t timestamp; | |
| 48 } HistoryItem; | |
| 49 | |
| 50 typedef enum { | |
| 51 ActiveTab_JSON = 0 | |
| 52 ActiveTab_Headers, | |
| 53 ActiveTab_Params, | |
| 54 } ActiveTab | |
| 55 | |
| 56 // Callback function for curl to write response data | |
| 57 static size_t Postdog_Curl_Callback(void *contents, size_t size, size_t nmemb, void *userp) | |
| 58 { | |
| 59 size_t real_size = size * nmemb; | |
| 60 ResponseBuffer *buf = (ResponseBuffer *)userp; | |
| 61 | |
| 62 char *ptr = realloc(buf->data, buf->size + real_size + 1); | |
| 63 if (ptr == NULL) | |
| 64 { | |
| 65 printf("Not enough memory for response\n"); | |
| 66 return 0; | |
| 67 } | |
| 68 | |
| 69 buf->data = ptr; | |
| 70 memcpy(&(buf->data[buf->size]), contents, real_size); | |
| 71 buf->size += real_size; | |
| 72 buf->data[buf->size] = 0; | |
| 73 | |
| 74 return real_size; | |
| 75 } | |
| 76 | |
| 77 // Function to make HTTP request using curl | |
| 78 int PostDog_Make_HttpRequest(const char *url, const char *method, const char *headers, | |
| 79 const char *body, char *response, size_t responseSize) | |
| 80 { | |
| 81 CURL *curl; | |
| 82 CURLcode res; | |
| 83 ResponseBuffer buffer = { .data = malloc(1), .size = 0 }; | |
| 84 | |
| 85 if (buffer.data == NULL) | |
| 86 { | |
| 87 snprintf(response, responseSize, "Error: Failed to allocate memory"); | |
| 88 return -1; | |
| 89 } | |
| 90 buffer.data[0] = '\0'; | |
| 91 | |
| 92 curl_global_init(CURL_GLOBAL_DEFAULT); | |
| 93 curl = curl_easy_init(); | |
| 94 | |
| 95 if (curl) | |
| 96 { | |
| 97 struct curl_slist *headerList = NULL; | |
| 98 | |
| 99 // Set URL | |
| 100 curl_easy_setopt(curl, CURLOPT_URL, url); | |
| 101 | |
| 102 // Set HTTP method | |
| 103 if (strcmp(method, "POST") == 0) | |
| 104 { | |
| 105 curl_easy_setopt(curl, CURLOPT_POST, 1L); | |
| 106 if (body && strlen(body) > 0) { | |
| 107 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); | |
| 108 } | |
| 109 } | |
| 110 else if (strcmp(method, "PUT") == 0) | |
| 111 { | |
| 112 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
| 113 if (body && strlen(body) > 0) { | |
| 114 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); | |
| 115 } | |
| 116 } | |
| 117 else if (strcmp(method, "DELETE") == 0) | |
| 118 { | |
| 119 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
| 120 } | |
| 121 // Default is GET | |
| 122 | |
| 123 // Parse and add headers | |
| 124 if (headers && strlen(headers) > 0) | |
| 125 { | |
| 126 char *headersCopy = strdup(headers); | |
| 127 char *line = strtok(headersCopy, "\n"); | |
| 128 while (line != NULL) { | |
| 129 // Trim whitespace | |
| 130 while (*line == ' ' || *line == '\t') line++; | |
| 131 if (strlen(line) > 0) { | |
| 132 headerList = curl_slist_append(headerList, line); | |
| 133 } | |
| 134 line = strtok(NULL, "\n"); | |
| 135 } | |
| 136 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList); | |
| 137 free(headersCopy); | |
| 138 } | |
| 139 | |
| 140 // Set write callback | |
| 141 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Postdog_Curl_Callback); | |
| 142 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buffer); | |
| 143 | |
| 144 // Follow redirects | |
| 145 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | |
| 146 | |
| 147 // Set timeout | |
| 148 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L); | |
| 149 | |
| 150 // Perform request | |
| 151 res = curl_easy_perform(curl); | |
| 152 | |
| 153 if (res != CURLE_OK) { | |
| 154 snprintf(response, responseSize, "Error: %s\n", curl_easy_strerror(res)); | |
| 155 } else { | |
| 156 long response_code; | |
| 157 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); | |
| 158 | |
| 159 snprintf(response, responseSize, "HTTP Status: %ld\n\n%s", | |
| 160 response_code, buffer.data ? buffer.data : ""); | |
| 161 } | |
| 162 | |
| 163 // Cleanup | |
| 164 if (headerList) curl_slist_free_all(headerList); | |
| 165 curl_easy_cleanup(curl); | |
| 166 } else { | |
| 167 snprintf(response, responseSize, "Error: Failed to initialize curl"); | |
| 168 } | |
| 169 | |
| 170 free(buffer.data); | |
| 171 curl_global_cleanup(); | |
| 172 | |
| 173 return 0; | |
| 174 } | |
| 175 | |
| 176 void PostDog_HistoryDirectory_Exists() | |
| 177 { | |
| 178 struct stat st = {0}; | |
| 179 if (stat("history", &st) == -1) | |
| 180 { | |
| 181 mkdir("history", 0700); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 int PostDog_HistoryDistory_ItemsLoad(HistoryItem *items, int maxItems) | |
| 186 { | |
| 187 DIR *dir = opendir("history"); | |
| 188 if (!dir) return 0; | |
| 189 | |
| 190 struct dirent *entry; | |
| 191 int count = 0; | |
| 192 | |
| 193 while ((entry = readdir(dir)) != NULL && count < maxItems) | |
| 194 { | |
| 195 if (entry->d_name[0] == '.') continue; | |
| 196 if (strstr(entry->d_name, ".txt") == NULL) continue; | |
| 197 | |
| 198 // Parse filename: YYYYMMDD_HHMMSS_METHOD.txt | |
| 199 strncpy(items[count].filename, entry->d_name, sizeof(items[count].filename) - 1); | |
| 200 | |
| 201 // Extract method from filename | |
| 202 char *methodStart = strrchr(entry->d_name, '_'); | |
| 203 if (methodStart) | |
| 204 { | |
| 205 methodStart++; // Skip underscore | |
| 206 char *dotPos = strchr(methodStart, '.'); | |
| 207 if (dotPos) { | |
| 208 int len = dotPos - methodStart; | |
| 209 if (len < 16) { | |
| 210 strncpy(items[count].method, methodStart, len); | |
| 211 items[count].method[len] = '\0'; | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 // Create display name: METHOD - YYYYMMDD HHMMSS | |
| 217 char dateTime[32] = ""; | |
| 218 if (strlen(entry->d_name) >= 15) | |
| 219 { | |
| 220 snprintf(dateTime, sizeof(dateTime), "%.8s %.6s", | |
| 221 entry->d_name, entry->d_name + 9); | |
| 222 } | |
| 223 snprintf(items[count].displayName, sizeof(items[count].displayName), | |
| 224 "%s - %s", items[count].method, dateTime); | |
| 225 count++; | |
| 226 } | |
| 227 | |
| 228 closedir(dir); | |
| 229 return count; | |
| 230 } | |
| 231 | |
| 232 int PostDog_HistoryDirectory_LoadRequest(const char *filename, char *url, char *method, char *headers, char *body) | |
| 233 { | |
| 234 char filepath[512]; | |
| 235 snprintf(filepath, sizeof(filepath), "history/%s", filename); | |
| 236 | |
| 237 FILE *f = fopen(filepath, "r"); | |
| 238 if (!f) return -1; | |
| 239 | |
| 240 char line[2048]; | |
| 241 int section = 0; // 0=url, 1=method, 2=headers, 3=body | |
| 242 | |
| 243 url[0] = '\0'; | |
| 244 method[0] = '\0'; | |
| 245 headers[0] = '\0'; | |
| 246 body[0] = '\0'; | |
| 247 | |
| 248 while (fgets(line, sizeof(line), f)) | |
| 249 { | |
| 250 // Remove newline | |
| 251 line[strcspn(line, "\n")] = 0; | |
| 252 | |
| 253 if (section == 0) | |
| 254 { | |
| 255 // First line is URL | |
| 256 strncpy(url, line, 1024 - 1); | |
| 257 section = 1; | |
| 258 } | |
| 259 else if (strncmp(line, "Method: ", 8) == 0) | |
| 260 { | |
| 261 strncpy(method, line + 8, 15); | |
| 262 section = 2; | |
| 263 } | |
| 264 else if (strncmp(line, "Headers:", 8) == 0) | |
| 265 { | |
| 266 section = 2; | |
| 267 } | |
| 268 else if (strcmp(line, "---") == 0) | |
| 269 { | |
| 270 section = 3; | |
| 271 } | |
| 272 else if (strncmp(line, "Body:", 5) == 0) | |
| 273 { | |
| 274 section = 3; | |
| 275 } | |
| 276 else if (section == 2 && strcmp(line, "None") != 0) | |
| 277 { | |
| 278 // Add header line | |
| 279 if (strlen(headers) > 0) strcat(headers, "\n"); | |
| 280 strncat(headers, line, HEADER_INPUT_BUFFER_LEN - strlen(headers) - 1); | |
| 281 } | |
| 282 else if (section == 3 && strcmp(line, "None") != 0) | |
| 283 { | |
| 284 // Add body line | |
| 285 if (strlen(body) > 0) strcat(body, "\n"); | |
| 286 strncat(body, line, JSON_INPUT_BUFFER_LEN - strlen(body) - 1); | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 fclose(f); | |
| 291 return 0; | |
| 292 } | |
| 293 | |
| 294 void updateUrlWithParams(char *url, size_t urlSize, const char *baseUrl, const char *params) | |
| 295 { | |
| 296 // Find if there's already a ? in the URL | |
| 297 char *questionMark = strchr(baseUrl, '?'); | |
| 298 | |
| 299 if (questionMark != NULL) { | |
| 300 // URL already has params, just copy the base | |
| 301 strncpy(url, baseUrl, urlSize - 1); | |
| 302 } else { | |
| 303 // No params yet, add them | |
| 304 snprintf(url, urlSize, "%s", baseUrl); | |
| 305 } | |
| 306 | |
| 307 // Parse and append params | |
| 308 if (params && strlen(params) > 0) { | |
| 309 char *paramsCopy = strdup(params); | |
| 310 char *line = strtok(paramsCopy, "\n"); | |
| 311 bool firstParam = (questionMark == NULL); | |
| 312 | |
| 313 while (line != NULL) { | |
| 314 // Trim whitespace | |
| 315 while (*line == ' ' || *line == '\t') line++; | |
| 316 | |
| 317 if (strlen(line) > 0 && strchr(line, '=')) { | |
| 318 size_t currentLen = strlen(url); | |
| 319 if (currentLen + 2 < urlSize) { | |
| 320 strcat(url, firstParam ? "?" : "&"); | |
| 321 firstParam = false; | |
| 322 strncat(url, line, urlSize - strlen(url) - 1); | |
| 323 } | |
| 324 } | |
| 325 line = strtok(NULL, "\n"); | |
| 326 } | |
| 327 free(paramsCopy); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 // Save request to history file | |
| 332 void saveRequestToHistory(const char *url, const char *method, const char *headers, const char *body) | |
| 333 { | |
| 334 PostDog_HistoryDirectory_Exists(); | |
| 335 | |
| 336 // Generate filename with timestamp | |
| 337 time_t now = time(NULL); | |
| 338 struct tm *t = localtime(&now); | |
| 339 char filename[256]; | |
| 340 snprintf(filename, sizeof(filename), "history/%04d%02d%02d_%02d%02d%02d_%s.txt", | |
| 341 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, | |
| 342 t->tm_hour, t->tm_min, t->tm_sec, method); | |
| 343 | |
| 344 FILE *f = fopen(filename, "w"); | |
| 345 if (f == NULL) { | |
| 346 printf("Failed to create history file\n"); | |
| 347 return; | |
| 348 } | |
| 349 | |
| 350 // Write URL | |
| 351 fprintf(f, "%s\n", url); | |
| 352 | |
| 353 // Write method | |
| 354 fprintf(f, "Method: %s\n", method); | |
| 355 | |
| 356 // Write headers | |
| 357 fprintf(f, "Headers:\n"); | |
| 358 if (headers && strlen(headers) > 0) { | |
| 359 fprintf(f, "%s\n", headers); | |
| 360 } else { | |
| 361 fprintf(f, "None\n"); | |
| 362 } | |
| 363 | |
| 364 fprintf(f, "---\n"); | |
| 365 | |
| 366 // Write body | |
| 367 fprintf(f, "Body:\n"); | |
| 368 if (body && strlen(body) > 0) { | |
| 369 fprintf(f, "%s\n", body); | |
| 370 } else { | |
| 371 fprintf(f, "None\n"); | |
| 372 } | |
| 373 | |
| 374 fclose(f); | |
| 375 printf("Request saved to %s\n", filename); | |
| 376 } | |
| 377 | |
| 378 void PostDog_Render_TextWithScroll(Rectangle textArea, Vector2 scroll, char * input) { | |
| 379 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); | |
| 380 | |
| 381 int yPos = textArea.y + 5 - (int)scroll.y; | |
| 382 char *textCopy = strdup(input); | |
| 383 char *line = strtok(textCopy, "\n"); | |
| 384 | |
| 385 while (line != NULL && yPos < textArea.y + textArea.height) | |
| 386 { | |
| 387 if (yPos + LINE_HEIGHT > textArea.y) { | |
| 388 DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 389 } | |
| 390 yPos += LINE_HEIGHT; | |
| 391 line = strtok(NULL, "\n"); | |
| 392 } | |
| 393 free(textCopy); | |
| 394 | |
| 395 EndScissorMode(); | |
| 396 } | |
| 397 | |
| 398 int main() | |
| 399 { | |
| 400 InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "PostDog - HTTP Client"); | |
| 401 SetWindowState(FLAG_WINDOW_RESIZABLE); | |
| 402 SetTargetFPS(60); | |
| 403 | |
| 404 PostDog_HistoryDirectory_Exists(); | |
| 405 | |
| 406 // UI State | |
| 407 char urlInput[1024] = "https://httpbin.org/get"; | |
| 408 bool urlEditMode = false; | |
| 409 | |
| 410 char jsonInput[JSON_INPUT_BUFFER_LEN] = "{\"key\":\"value\"}"; | |
| 411 bool jsonEditMode = false; | |
| 412 | |
| 413 char headersInput[HEADER_INPUT_BUFFER_LEN] = "Content-Type: application/json"; | |
| 414 bool headersEditMode = false; | |
| 415 | |
| 416 char responseText[16384] = "Response will appear here...\n\nTry the default URL or enter your own!"; | |
| 417 | |
| 418 char paramsInput[PARAM_INPUT_BUFFER_LEN] = "key1=value1\nkey2=value2"; | |
| 419 bool paramsEditMode = false; | |
| 420 | |
| 421 ActiveTab activeTab = ActiveTab_JSON; // 0 = JSON, 1 = Headers, 2 = Params | |
| 422 | |
| 423 // HTTP method selection | |
| 424 int methodActive = 0; | |
| 425 bool methodDropdown = false; | |
| 426 const char *methods[] = { "GET", "POST", "PUT", "DELETE" }; | |
| 427 | |
| 428 // Scroll support | |
| 429 Vector2 jsonScroll = { 0, 0 }; | |
| 430 Vector2 headersScroll = { 0, 0 }; | |
| 431 Vector2 paramsScroll = { 0, 0 }; | |
| 432 Vector2 responseScroll = { 0, 0 }; | |
| 433 Vector2 historyScroll = { 0, 0 }; | |
| 434 | |
| 435 // History | |
| 436 HistoryItem historyItems[MAX_HISTORY_ITEMS]; | |
| 437 int historyCount = 0; | |
| 438 int selectedHistoryIndex = -1; | |
| 439 | |
| 440 // Load initial history | |
| 441 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 442 | |
| 443 while (!WindowShouldClose()) | |
| 444 { | |
| 445 // Get current window dimensions for responsive layout | |
| 446 int screenWidth = GetScreenWidth(); | |
| 447 int screenHeight = GetScreenHeight(); | |
| 448 | |
| 449 // Layout calculations | |
| 450 Rectangle sidebar = { 0, 10, SIDEBAR_WIDTH, screenHeight }; | |
| 451 | |
| 452 float mainX = SIDEBAR_WIDTH + PADDING; | |
| 453 float mainWidth = screenWidth - SIDEBAR_WIDTH - PADDING * 2; | |
| 454 | |
| 455 // URL input box - leave space for SEND button on the right | |
| 456 Rectangle urlBox = { | |
| 457 mainX, | |
| 458 PADDING, | |
| 459 mainWidth - SEND_BUTTON_WIDTH - PADDING, | |
| 460 URL_INPUT_HEIGHT | |
| 461 }; | |
| 462 | |
| 463 // SEND button positioned beside URL input | |
| 464 Rectangle sendButton = { | |
| 465 urlBox.x + urlBox.width + PADDING, | |
| 466 PADDING, | |
| 467 SEND_BUTTON_WIDTH, | |
| 468 SEND_BUTTON_HEIGHT | |
| 469 }; | |
| 470 | |
| 471 // Method dropdown below URL | |
| 472 Rectangle methodButton = { | |
| 473 mainX, | |
| 474 urlBox.y + urlBox.height + PADDING, | |
| 475 METHOD_BUTTON_WIDTH, | |
| 476 METHOD_BUTTON_HEIGHT | |
| 477 }; | |
| 478 | |
| 479 float tabHeight = 30; | |
| 480 float contentY = methodButton.y + methodButton.height + PADDING + tabHeight; | |
| 481 float contentHeight = screenHeight - contentY - PADDING; | |
| 482 | |
| 483 float panelWidth = (mainWidth - PADDING) / 2; | |
| 484 | |
| 485 Rectangle tabBar = { | |
| 486 mainX, | |
| 487 methodButton.y + methodButton.height + PADDING, | |
| 488 panelWidth, | |
| 489 tabHeight | |
| 490 }; | |
| 491 | |
| 492 Rectangle requestPanel = { | |
| 493 mainX, | |
| 494 contentY, | |
| 495 panelWidth, | |
| 496 contentHeight | |
| 497 }; | |
| 498 | |
| 499 Rectangle responsePanel = { | |
| 500 mainX + panelWidth + PADDING, | |
| 501 contentY, | |
| 502 panelWidth, | |
| 503 contentHeight | |
| 504 }; | |
| 505 | |
| 506 BeginDrawing(); | |
| 507 ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR))); | |
| 508 | |
| 509 // Sidebar with history | |
| 510 DrawRectangleRec(sidebar, Fade(GRAY, 0.1f)); | |
| 511 GuiGroupBox(sidebar, "HISTORY"); | |
| 512 | |
| 513 // Refresh history button | |
| 514 Rectangle refreshBtn = { sidebar.x + 5, sidebar.y + 5, 90, 20 }; | |
| 515 if (GuiButton(refreshBtn, "Refresh")) { | |
| 516 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 517 } | |
| 518 | |
| 519 // Scroll area for history | |
| 520 Rectangle historyArea = { sidebar.x + 5, sidebar.y + 30, sidebar.width - 10, sidebar.height - 40 }; | |
| 521 | |
| 522 // Handle scroll | |
| 523 if (CheckCollisionPointRec(GetMousePosition(), historyArea)) { | |
| 524 float wheel = GetMouseWheelMove(); | |
| 525 historyScroll.y += wheel * 20; | |
| 526 if (historyScroll.y < 0) historyScroll.y = 0; | |
| 527 } | |
| 528 | |
| 529 BeginScissorMode(historyArea.x, historyArea.y, historyArea.width, historyArea.height); | |
| 530 | |
| 531 if (historyCount == 0) { | |
| 532 DrawText("No requests yet", historyArea.x + 5, historyArea.y + 5, 10, DARKGRAY); | |
| 533 } else { | |
| 534 int yPos = historyArea.y + 5 - (int)historyScroll.y; | |
| 535 int itemHeight = 45; | |
| 536 | |
| 537 for (int i = 0; i < historyCount; i++) { | |
| 538 if (yPos > historyArea.y - itemHeight && yPos < historyArea.y + historyArea.height) { | |
| 539 Rectangle itemRect = { historyArea.x, yPos, historyArea.width, itemHeight - 2 }; | |
| 540 | |
| 541 // Draw button for history item | |
| 542 Color bgColor = (selectedHistoryIndex == i) ? Fade(BLUE, 0.3f) : Fade(LIGHTGRAY, 0.5f); | |
| 543 | |
| 544 if (CheckCollisionPointRec(GetMousePosition(), itemRect) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { | |
| 545 // Load this request | |
| 546 char tempUrl[1024], tempMethod[16], tempHeaders[HEADER_INPUT_BUFFER_LEN], tempBody[JSON_INPUT_BUFFER_LEN]; | |
| 547 | |
| 548 if (PostDog_HistoryDirectory_LoadRequest( | |
| 549 historyItems[i].filename, tempUrl, tempMethod, tempHeaders, tempBody) == 0) | |
| 550 { | |
| 551 strncpy(urlInput, tempUrl, sizeof(urlInput) - 1); | |
| 552 strncpy(headersInput, tempHeaders, sizeof(headersInput) - 1); | |
| 553 strncpy(jsonInput, tempBody, sizeof(jsonInput) - 1); | |
| 554 | |
| 555 // Set method | |
| 556 for (int m = 0; m < 4; m++) { | |
| 557 if (strcmp(methods[m], tempMethod) == 0) { | |
| 558 methodActive = m; | |
| 559 break; | |
| 560 } | |
| 561 } | |
| 562 | |
| 563 selectedHistoryIndex = i; | |
| 564 strcpy(responseText, "Request loaded from history. Click SEND to execute."); | |
| 565 } | |
| 566 } | |
| 567 | |
| 568 DrawRectangleRec(itemRect, bgColor); | |
| 569 DrawRectangleLinesEx(itemRect, 1, GRAY); | |
| 570 | |
| 571 // Draw method badge | |
| 572 DrawText(historyItems[i].method, itemRect.x + 5, yPos + 5, 10, BLACK); | |
| 573 | |
| 574 // Draw timestamp (date only) | |
| 575 char dateStr[16] = ""; | |
| 576 if (strlen(historyItems[i].filename) >= 8) { | |
| 577 snprintf(dateStr, sizeof(dateStr), "%.4s-%.2s-%.2s", | |
| 578 historyItems[i].filename, historyItems[i].filename + 4, historyItems[i].filename + 6); | |
| 579 } | |
| 580 DrawText(dateStr, itemRect.x + 5, yPos + 18, 8, DARKGRAY); | |
| 581 | |
| 582 // Draw time | |
| 583 char timeStr[16] = ""; | |
| 584 if (strlen(historyItems[i].filename) >= 15) { | |
| 585 snprintf(timeStr, sizeof(timeStr), "%.2s:%.2s:%.2s", | |
| 586 historyItems[i].filename + 9, historyItems[i].filename + 11, historyItems[i].filename + 13); | |
| 587 } | |
| 588 DrawText(timeStr, itemRect.x + 5, yPos + 28, 8, DARKGRAY); | |
| 589 } | |
| 590 | |
| 591 yPos += itemHeight; | |
| 592 } | |
| 593 } | |
| 594 | |
| 595 EndScissorMode(); | |
| 596 | |
| 597 // URL Input | |
| 598 GuiLabel((Rectangle){ mainX, PADDING - 15, 100, 20 }, "URL:"); | |
| 599 if (GuiTextBox(urlBox, urlInput, 1024, urlEditMode)) | |
| 600 { | |
| 601 urlEditMode = !urlEditMode; | |
| 602 } | |
| 603 // Copy support for URL | |
| 604 if (urlEditMode && (IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) { | |
| 605 SetClipboardText(urlInput); | |
| 606 } | |
| 607 | |
| 608 // Send button (beside URL) | |
| 609 if (GuiButton(sendButton, "SEND")) | |
| 610 { | |
| 611 strcpy(responseText, "Sending request...\n"); | |
| 612 | |
| 613 // Make the actual HTTP request | |
| 614 char tempResponse[16384]; | |
| 615 const char *selectedMethod = methods[methodActive]; | |
| 616 | |
| 617 // Use JSON body for POST/PUT, otherwise use empty body | |
| 618 const char *requestBody = (methodActive == 1 || methodActive == 2) ? jsonInput : NULL; | |
| 619 const char *requestHeaders = headersInput; | |
| 620 | |
| 621 // Save request to history | |
| 622 saveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody); | |
| 623 | |
| 624 // Reload history to show the new request | |
| 625 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 626 | |
| 627 PostDog_Make_HttpRequest(urlInput, selectedMethod, requestHeaders, | |
| 628 requestBody, tempResponse, sizeof(tempResponse)); | |
| 629 | |
| 630 strncpy(responseText, tempResponse, sizeof(responseText) - 1); | |
| 631 responseText[sizeof(responseText) - 1] = '\0'; | |
| 632 } | |
| 633 | |
| 634 // Tab toggle (3 tabs now) | |
| 635 float tabWidth = tabBar.width / 3; | |
| 636 Rectangle jsonTab = { tabBar.x, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 637 Rectangle headersTab = { tabBar.x + tabWidth, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 638 Rectangle paramsTab = { tabBar.x + tabWidth * 2, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 639 | |
| 640 if (GuiButton(jsonTab, activeTab == ActiveTab_JSON ? "#191#Body" : "Body")) | |
| 641 { | |
| 642 activeTab = ActiveTab_JSON; | |
| 643 } | |
| 644 | |
| 645 if (GuiButton(headersTab, activeTab == ActiveTab_Headers ? "#191#Headers" : "Headers")) | |
| 646 { | |
| 647 activeTab = ActiveTab_Headers; | |
| 648 } | |
| 649 | |
| 650 if (GuiButton(paramsTab, activeTab == ActiveTab_Params ? "#191#Params" : "Params")) | |
| 651 { | |
| 652 activeTab = ActiveTab_Headers; | |
| 653 } | |
| 654 | |
| 655 const char *panelTitle; | |
| 656 switch(activeTab) { | |
| 657 case ActiveTab_JSON: | |
| 658 { | |
| 659 panelTitle = "Request Body (JSON)" | |
| 660 } | |
| 661 case ActiveTab_Headers: | |
| 662 { | |
| 663 panelTitle = "Request Headers" | |
| 664 } | |
| 665 case ActiveTab_Params: | |
| 666 { | |
| 667 panelTitle = "Query Parameters" | |
| 668 } | |
| 669 } | |
| 670 | |
| 671 // Panel title | |
| 672 GuiGroupBox(requestPanel, panelTitle); | |
| 673 Rectangle textArea = { | |
| 674 requestPanel.x + 10, | |
| 675 requestPanel.y + 30, | |
| 676 requestPanel.width - 20, | |
| 677 requestPanel.height - 40 | |
| 678 }; | |
| 679 | |
| 680 // Handle click outside to disable edit mode | |
| 681 if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) | |
| 682 { | |
| 683 if (!CheckCollisionPointRec(GetMousePosition(), textArea)) | |
| 684 { | |
| 685 jsonEditMode = false; | |
| 686 headersEditMode = false; | |
| 687 paramsEditMode = false; | |
| 688 } | |
| 689 } | |
| 690 | |
| 691 // Draw border for text area | |
| 692 DrawRectangleLinesEx(textArea, 1, GRAY); | |
| 693 | |
| 694 // Manual scroll handling with mouse wheel | |
| 695 if (CheckCollisionPointRec(GetMousePosition(), textArea)) | |
| 696 { | |
| 697 float wheel = GetMouseWheelMove(); | |
| 698 switch(activeTab) { | |
| 699 case ActiveTab_JSON: | |
| 700 { | |
| 701 jsonScroll.y += wheel * 20; | |
| 702 if (jsonScroll.y < 0) jsonScroll.y = 0; | |
| 703 } | |
| 704 case ActiveTab_Headers: | |
| 705 { | |
| 706 headersScroll.y += wheel * 20; | |
| 707 if (headersScroll.y < 0) headersScroll.y = 0; | |
| 708 } | |
| 709 case ActiveTab_Params: | |
| 710 { | |
| 711 paramsScroll.y += wheel * 20; | |
| 712 if (paramsScroll.y < 0) paramsScroll.y = 0; | |
| 713 } | |
| 714 } | |
| 715 } | |
| 716 if (activeTab == 0) | |
| 717 { | |
| 718 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); | |
| 719 | |
| 720 int yPos = textArea.y + 5 - (int)jsonScroll.y; | |
| 721 char *textCopy = strdup(jsonInput); | |
| 722 char *line = strtok(textCopy, "\n"); | |
| 723 | |
| 724 while (line != NULL && yPos < textArea.y + textArea.height) | |
| 725 { | |
| 726 if (yPos + LINE_HEIGHT > textArea.y) { | |
| 727 DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 728 } | |
| 729 yPos += LINE_HEIGHT; | |
| 730 line = strtok(NULL, "\n"); | |
| 731 } | |
| 732 free(textCopy); | |
| 733 | |
| 734 EndScissorMode(); | |
| 735 | |
| 736 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) { | |
| 737 SetClipboardText(jsonInput); | |
| 738 } | |
| 739 | |
| 740 // Edit button | |
| 741 Rectangle editBtn = { textArea.x + textArea.width - 60, textArea.y + textArea.height - 10, 50, 20 }; | |
| 742 if (GuiButton(editBtn, "Edit")) { | |
| 743 jsonEditMode = !jsonEditMode; | |
| 744 } | |
| 745 } else if (activeTab == 1) { | |
| 746 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); | |
| 747 | |
| 748 int yPos = textArea.y + 5 - (int)headersScroll.y; | |
| 749 char *textCopy = strdup(headersInput); | |
| 750 char *line = strtok(textCopy, "\n"); | |
| 751 | |
| 752 while (line != NULL && yPos < textArea.y + textArea.height) | |
| 753 { | |
| 754 if (yPos + LINE_HEIGHT > textArea.y) | |
| 755 { | |
| 756 DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 757 } | |
| 758 yPos += LINE_HEIGHT; | |
| 759 line = strtok(NULL, "\n"); | |
| 760 } | |
| 761 free(textCopy); | |
| 762 | |
| 763 EndScissorMode(); | |
| 764 | |
| 765 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) { | |
| 766 SetClipboardText(headersInput); | |
| 767 } | |
| 768 | |
| 769 // Edit button | |
| 770 Rectangle editBtn = { textArea.x + textArea.width - 60, textArea.y + textArea.height + 5, 50, 20 }; | |
| 771 if (GuiButton(editBtn, "Edit")) { | |
| 772 headersEditMode = !headersEditMode; | |
| 773 } | |
| 774 } else { | |
| 775 // Params input with manual scroll | |
| 776 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); | |
| 777 | |
| 778 int yPos = textArea.y + 5 - (int)paramsScroll.y; | |
| 779 char *textCopy = strdup(paramsInput); | |
| 780 char *line = strtok(textCopy, "\n"); | |
| 781 | |
| 782 while (line != NULL && yPos < textArea.y + textArea.height) { | |
| 783 if (yPos + LINE_HEIGHT > textArea.y) { | |
| 784 DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 785 } | |
| 786 yPos += LINE_HEIGHT; | |
| 787 line = strtok(NULL, "\n"); | |
| 788 } | |
| 789 free(textCopy); | |
| 790 | |
| 791 EndScissorMode(); | |
| 792 | |
| 793 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) { | |
| 794 SetClipboardText(paramsInput); | |
| 795 } | |
| 796 | |
| 797 // Buttons | |
| 798 Rectangle editBtn = { textArea.x + textArea.width - 60, textArea.y + textArea.height + 5, 50, 20 }; | |
| 799 if (GuiButton(editBtn, "Edit")) | |
| 800 { | |
| 801 paramsEditMode = !paramsEditMode; | |
| 802 } | |
| 803 | |
| 804 Rectangle updateUrlBtn = { textArea.x, textArea.y + textArea.height + 5, 120, 20 }; | |
| 805 if (GuiButton(updateUrlBtn, "Update URL")) | |
| 806 { | |
| 807 char tempUrl[1024]; | |
| 808 strncpy(tempUrl, urlInput, sizeof(tempUrl) - 1); | |
| 809 // Remove existing params if any | |
| 810 char *questionMark = strchr(tempUrl, '?'); | |
| 811 if (questionMark) *questionMark = '\0'; | |
| 812 | |
| 813 updateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput); | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 // Response Panel with scroll | |
| 818 GuiGroupBox(responsePanel, "Response"); | |
| 819 | |
| 820 Rectangle responseArea = { | |
| 821 responsePanel.x + 10, | |
| 822 responsePanel.y + 30, | |
| 823 responsePanel.width - 20, | |
| 824 responsePanel.height - 40 | |
| 825 }; | |
| 826 | |
| 827 // Manual scroll for response | |
| 828 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) | |
| 829 { | |
| 830 float wheel = GetMouseWheelMove(); | |
| 831 responseScroll.y += wheel * 20; | |
| 832 if (responseScroll.y < 0) responseScroll.y = 0; | |
| 833 } | |
| 834 | |
| 835 // Draw border | |
| 836 DrawRectangleLinesEx(responseArea, 1, GRAY); | |
| 837 | |
| 838 // Draw response text with scroll | |
| 839 BeginScissorMode(responseArea.x, responseArea.y, responseArea.width, responseArea.height); | |
| 840 | |
| 841 int yPos = responseArea.y + 5 - (int)responseScroll.y; | |
| 842 char *textCopy = strdup(responseText); | |
| 843 char *line = strtok(textCopy, "\n"); | |
| 844 | |
| 845 while (line != NULL && yPos < responseArea.y + responseArea.height) | |
| 846 { | |
| 847 if (yPos + LINE_HEIGHT > responseArea.y) | |
| 848 { | |
| 849 DrawText(line, responseArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 850 } | |
| 851 yPos += LINE_HEIGHT; | |
| 852 line = strtok(NULL, "\n"); | |
| 853 } | |
| 854 free(textCopy); | |
| 855 | |
| 856 EndScissorMode(); | |
| 857 | |
| 858 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) | |
| 859 { | |
| 860 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) { | |
| 861 SetClipboardText(responseText); | |
| 862 } | |
| 863 } | |
| 864 | |
| 865 // --- Edit modal ---- | |
| 866 if (jsonEditMode) | |
| 867 { | |
| 868 GuiTextBox( | |
| 869 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 870 jsonInput, JSON_INPUT_BUFFER_LEN, true); | |
| 871 } | |
| 872 | |
| 873 if (headersEditMode) | |
| 874 { | |
| 875 GuiTextBox( | |
| 876 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 877 headersInput, HEADER_INPUT_BUFFER_LEN, true); | |
| 878 } | |
| 879 | |
| 880 if (paramsEditMode) | |
| 881 { | |
| 882 GuiTextBox( | |
| 883 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 884 paramsInput, PARAM_INPUT_BUFFER_LEN, true); | |
| 885 } | |
| 886 | |
| 887 if (GuiDropdownBox(methodButton, "GET;POST;PUT;DELETE", &methodActive, methodDropdown)) | |
| 888 { | |
| 889 methodDropdown = !methodDropdown; | |
| 890 } | |
| 891 | |
| 892 | |
| 893 EndDrawing(); | |
| 894 } | |
| 895 | |
| 896 CloseWindow(); | |
| 897 return 0; | |
| 898 } |