comparison postdog/main.c @ 60:d64a8c189a77

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