forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPcode.c
3093 lines (2364 loc) · 75.5 KB
/
HTTPcode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2001-2015 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#define DllImport
#include "CHeaders.h"
#include <stdlib.h>
#include "tncinfo.h"
#include "time.h"
#include "bpq32.h"
#include "telnetserver.h"
#define CKernel
#include "HTTPConnectionInfo.h"
extern int MAXBUFFS, QCOUNT, MINBUFFCOUNT, NOBUFFCOUNT, BUFFERWAITS, L3FRAMES;
extern int NUMBEROFNODES, MAXDESTS, L4CONNECTSOUT, L4CONNECTSIN, L4FRAMESTX, L4FRAMESRX, L4FRAMESRETRIED, OLDFRAMES;
extern int STATSTIME;
extern TRANSPORTENTRY * L4TABLE;
extern BPQVECSTRUC BPQHOSTVECTOR[];
extern BOOL APRSApplConnected;
extern char VersionString[];
VOID FormatTime2(char * Time, time_t cTime);
DllExport int APIENTRY Get_APPLMASK(int Stream);
VOID SaveUIConfig();
int ProcessNodeSignon(SOCKET sock, struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session);
VOID SetupUI(int Port);
VOID SendUIBeacon(int Port);
extern struct ROUTE * NEIGHBOURS;
extern int ROUTE_LEN;
extern int MAXNEIGHBOURS;
extern struct DEST_LIST * DESTS; // NODE LIST
extern int DEST_LIST_LEN;
extern int MAXDESTS; // MAX NODES IN SYSTEM
extern struct _LINKTABLE * LINKS;
extern int LINK_TABLE_LEN;
extern int MAXLINKS;
extern COLORREF Colours[256];
extern BOOL IncludesMail;
extern BOOL IncludesChat;
extern BOOL APRSWeb;
extern char * UIUIDigi[33];
extern char UIUIDEST[33][11]; // Dest for Beacons
extern UCHAR FN[33][256]; // Filename
extern int Interval[33]; // Beacon Interval (Mins)
extern char Message[33][1000]; // Beacon Text
extern int MinCounter[33]; // Interval Countdown
extern BOOL SendFromFile[33];
extern HKEY REGTREE;
extern BOOL APRSActive;
char * strlop(char * buf, char delim);
VOID sendandcheck(SOCKET sock, const char * Buffer, int Len);
int CompareNode(const void *a, const void *b);
int CompareAlias(const void *a, const void *b);
void ProcessMailHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen);
void ProcessChatHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen);
struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot);
int SetupNodeMenu(char * Buff);
int StatusProc(char * Buff);
int ProcessMailSignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session, BOOL WebMail);
int ProcessChatSignon(struct TCPINFO * TCP, char * MsgPtr, char * Appl, char * Reply, struct HTTPConnectionInfo ** Session);
VOID APRSProcessHTTPMessage(SOCKET sock, char * MsgPtr);
static struct HTTPConnectionInfo * SessionList; // active term mode sessions
char Mycall[10];
char APRSPipeFileName[] = "\\\\.\\pipe\\BPQAPRSWebPipe";
char MAILPipeFileName[] = "\\\\.\\pipe\\BPQMAILWebPipe";
char CHATPipeFileName[] = "\\\\.\\pipe\\BPQCHATWebPipe";
char Index[] = "<html><head><title>%s's BPQ32 Web Server</title></head><body><P align=center>"
"<table border=2 cellpadding=2 cellspacing=2 bgcolor=white>"
"<tr><td align=center><a href=/Node/NodeMenu.html>Node Pages</a></td>"
"<td align=center><a href=/aprs/all.html>APRS Pages</a></td></tr></table></body></html>";
char IndexNoAPRS[] = "<meta http-equiv=\"refresh\" content=\"0;url=/Node/NodeIndex.html\">"
"<html><head></head><body></body></html>";
char NodeMenuHeader[] = "<html><head><title>%s's BPQ32 Web Server</title><script>"
"function dev_win(URL,x,y){"
"var xx = \"width=\" + x;"
"var yy = \",height=\" + y;"
"var param = \"toolbar=no, location=no, directories=no, status=no, "
"menubar=no, scrollbars=no, resizable=no, titlebar=no, toobar=no, \" + xx + yy;"
"window.open(URL,\"_blank\",param);"
"}"
"function open_win(){";
char NodeMenuLine[] = "dev_win(\"/Node/Port?%d\",%d,%d);";
char NodeMenuRest[] = "}</script></head>"
"<body background=\"/background.jpg\"><h1 align=center>BPQ32 Node %s</h1><P>"
"<P align=center><table border=1 cellpadding=2 bgcolor=white><tr>"
"<td><a href=/Node/Routes.html>Routes</a></td>"
"<td><a href=/Node/Nodes.html>Nodes</a></td>"
"<td><a href=/Node/Ports.html>Ports</a></td>"
"<td><a href=/Node/Links.html>Links</a></td>"
"<td><a href=/Node/Users.html>Users</a></td>"
"<td><a href=/Node/Stats.html>Stats</a></td>"
"<td><a href=/Node/Terminal.html>Terminal</a></td>%s%s%s%s%s";
char DriverBit[] = "<td><a href=\"javascript:open_win();\">Driver Windows</a></td>"
"<td><a href=javascript:dev_win(\"/Node/Streams\",820,700);>Stream Status</a></td>";
char APRSBit[] = "<td><a href=../aprs/all.html>APRS Pages</a></td>";
char MailBit[] = "<td><a href=../Mail/Header>Mail Server Pages</a></td>";
char ChatBit[] = "<td><a href=../Chat/Header>Chat Server Pages</a></td>";
char NodeTail[] = "<td><a href=/Node/Signon.html>SYSOP Signin</a></td>"
"<td><a href=/Node/EditCfg.html>Edit Config</a></td>"
"</tr></table>";
char Tail[] = "</body></html>";
char RouteHddr[] = "<h2 align=center>Routes</h2><table align=center border=2 style=font-family:monospace bgcolor=white>"
"<tr><th>Port</th><th>Call</th><th>Quality</th><th>Node Count</th><th>Frame Count</th><th>Retries</th><th>Percent</th><th>Maxframe</th><th>Frack</th><th>Last Heard</th><th>Queued</th><th>Rem Qual</th></tr>";
char RouteLine[] = "<tr><td>%s%d</td><td>%s%c</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d%</td><td>%d</td><td>%d</td><td>%02d:%02d</td><td>%d</td><td>%d</td></tr>";
char xNodeHddr[] = "<align=center><form align=center method=get action=/Node/Nodes.html>"
"<table align=center bgcolor=white>"
"<tr><td><input type=submit name=a value=\"Nodes Sorted by Alias\"></td><td>"
"<input type=submit name=c value=\"Nodes Sorted by Call\"></td><td>"
"<input type=submit name=t value=\"Nodes with traffic\"></td></tr></form></table>"
"<h2 align=center>Nodes %s</h2><table style=font-family:monospace align=center border=2 bgcolor=white><tr>";
char NodeHddr[] = "<center><form method=get action=/Node/Nodes.html>"
"<input type=submit name=a value=\"Nodes Sorted by Alias\">"
"<input type=submit name=c value=\"Nodes Sorted by Call\">"
"<input type=submit name=t value=\"Nodes with traffic\"></form></center>"
"<h2 align=center>Nodes %s</h2><table style=font-family:monospace align=center border=2 bgcolor=white><tr>";
char NodeLine[] = "<td><a href=NodeDetail?%s>%s:%s</td>";
char PortsHddr[] = "<h2 align=center>Ports</h2><table align=center border=2 bgcolor=white>"
"<tr><th>Port</th><th>Driver</th><th>ID</th><th>Beacons</th></tr>";
char PortLine[] = "<tr><td>%d</td><td><a href=PortStats?%d&%s> %s</a></td><td>%s</td></tr>";
char PortLineWithBeacon[] = "<tr><td>%d</td><td><a href=PortStats?%d&%s> %s</a></td><td>%s</td><td><a href=PortBeacons?%d> Beacons</a></td></tr>";
char SessionPortLine[] = "<tr><td>%d</td><td>%s</td><td>%s</td><td> </td></tr>";
char StatsHddr[] = "<h2 align=center>Node Stats</h2><table align=center cellpadding=2 bgcolor=white>"
"<col width=250 /><col width=80 /><col width=80 /><col width=80 /><col width=80 /><col width=80 />";
char PortStatsHddr[] = "<h2 align=center>Stats for Port %d</h2><table align=center border=2 cellpadding=2 bgcolor=white>";
char PortStatsLine[] = "<tr><td> %s </td><td> %d </td></tr>";
char Beacons[] = "<h2 align=center>Beacon Configuration for Port %d</h2><h3 align=center>You need to be signed in to save changes</h3><table align=center border=2 cellpadding=2 bgcolor=white>"
"<form method=post action=BeaconAction>"
"<table align=center bgcolor=white>"
"<tr><td>Send Interval (Minutes)</td><td><input type=text name=Every tabindex=1 size=5 value=%d></td></tr>"
"<tr><td>To</td><td><input name=Dest tabindex=2 size=5 value=%s></td></tr>"
"<tr><td>Path</td><td><input type=text name=Path size=50 maxlength=50 value=%s></td></tr>"
"<tr><td>Send From File</td><td><input type=text name=File size=50 maxlength=50 value=%s></td></tr>"
"<tr><td>Text</td><td><textarea name=\"Text\" cols=40 rows=5>%s</textarea></td></tr>"
"</table>"
"<input type=hidden name=Port value=%d>"
"<p align=center><input type=submit value=Save><input type=submit value=Test name=Test>"
"</form>";
char LinkHddr[] = "<h2 align=center>Links</h2><table align=center border=2 bgcolor=white>"
"<tr><th>Far Call</th><th>Our Call</th><th>Port</th><th>ax.25 state</th><th>Link Type</th><th>ax.25 Version</th></tr>";
char LinkLine[] = "<tr><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td><td align=center >%d</td></tr>";
char UserHddr[] = "<h2 align=center>Sessions</h2><table align=center border=2 cellpadding=2 bgcolor=white>";
char UserLine[] = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>";
char TermSignon[] = "<html><head><title>BPQ32 Node %s Terminal Access</title></head><body background=\"/background.jpg\">"
"<h2 align=center>BPQ32 Node %s Terminal Access</h2>"
"<h3 align=center>Please enter username and password to access the node</h3>"
"<form method=post action=TermSignon>"
"<table align=center bgcolor=white>"
"<tr><td>User</td><td><input type=text name=user tabindex=1 size=20 maxlength=50 /></td></tr>"
"<tr><td>Password</td><td><input type=password name=password tabindex=2 size=20 maxlength=50 /></td></tr></table>"
"<p align=center><input type=submit value=Submit><input type=submit value=Cancel name=Cancel>"
"<input type=hidden name=Appl value=\"%s\" id=Pass></form>";
char PassError[] = "<p align=center>Sorry, User or Password is invalid - please try again</p>";
char BusyError[] = "<p align=center>Sorry, No sessions available - please try later</p>";
char LostSession[] = "<html><body>Sorry, Session had been lost - refresh page to sign in again";
char TermPage[] = "<html><meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />"
"<head><title>BPQ32 Node %s</title></head>"
"<body><h3 align=center>BPQ32 Node %s</h3>"
"<form method=post action=/Node/TermClose?%s>"
"<p align=center><input type=submit value=\"Close and return to Node Page\" /></form>"
"<iframe src=OutputScreen.html?%s width=100%% height=80%%></iframe>"
"<iframe src=InputLine.html?%s width=100%% height=60></iframe>"
"</body>";
char TermOutput[] = "<html><head>"
"<meta http-equiv=cache-control content=no-cache>"
"<meta http-equiv=pragma content=no-cache>"
"<meta http-equiv=expires content=0>"
"<meta http-equiv=refresh content=2>"
"<script type=\"text/javascript\">\r\n"
"function ScrollOutput()\r\n"
"{window.scrollBy(0,document.body.scrollHeight)}</script>"
"</head><body id=Text onload=\"ScrollOutput()\">"
"<p style=font-family:monospace>";
//<body onLoad="pageScroll()">
//char TermOutputTail[] = "</p><script>scrollElementToEnd(document.getElementById(\"Text\"));</script>";
//char TermOutputTail[] = "</p><script>document.getElementById(\"Text\").scrollTo(0,1500)</script>";
//char TermOutputTail[] = "</p><script>window.scrollBy(0,500);</script></body></html>";
char TermOutputTail[] = "</p></script></body></html>";
char InputLine[] = "<html><head></head><body>"
"<form name=inputform method=post action=/TermInput?%s>"
"<input type=text size=105 name=input />"
"<script>document.inputform.input.focus();</script></form>";
static char NodeSignon[] = "<html><head><title>BPQ32 Node SYSOP Access</title></head><body background=\"/background.jpg\">"
"<h3 align=center>BPQ32 Node %s SYSOP Access</h3>"
"<h3 align=center>This page sets Cookies. Don't continue if you object to this</h3>"
"<h3 align=center>Please enter Callsign and Password to access the Node</h3>"
"<form method=post action=/Node/Signon?Node>"
"<table align=center bgcolor=white>"
"<tr><td>User</td><td><input type=text name=user tabindex=1 size=20 maxlength=50 /></td></tr>"
"<tr><td>Password</td><td><input type=password name=password tabindex=2 size=20 maxlength=50 /></td></tr></table>"
"<p align=center><input type=submit value=Submit /><input type=submit value=Cancel name=Cancel /></form>";
static char MailSignon[] = "<html><head><title>BPQ32 Mail Server Access</title></head><body background=\"/background.jpg\">"
"<h3 align=center>BPQ32 Mail Server %s Access</h3>"
"<h3 align=center>Please enter Callsign and Password to access the BBS</h3>"
"<form method=post action=/Mail/Signon?Mail>"
"<table align=center bgcolor=white>"
"<tr><td>User</td><td><input type=text name=user tabindex=1 size=20 maxlength=50 /></td></tr>"
"<tr><td>Password</td><td><input type=password name=password tabindex=2 size=20 maxlength=50 /></td></tr></table>"
"<p align=center><input type=submit value=Submit /><input type=submit value=Cancel name=Cancel /></form>";
static char ChatSignon[] = "<html><head><title>BPQ32 Chat Server Access</title></head><body background=\"/background.jpg\">"
"<h3 align=center>BPQ32 Chat Server %s Access</h3>"
"<h3 align=center>Please enter Callsign and Password to access the Chat Server</h3>"
"<form method=post action=/Chat/Signon?Chat>"
"<table align=center bgcolor=white>"
"<tr><td>User</td><td><input type=text name=user tabindex=1 size=20 maxlength=50 /></td></tr>"
"<tr><td>Password</td><td><input type=password name=password tabindex=2 size=20 maxlength=50 /></td></tr></table>"
"<p align=center><input type=submit value=Submit /><input type=submit value=Cancel name=Cancel /></form>";
static char MailLostSession[] = "<html><body>"
"<form style=\"font-family: monospace; text-align: center;\" method=post action=/Mail/Lost?%s>"
"Sorry, Session had been lost<br><br> "
"<input name=Submit value=Restart type=submit> <input type=submit value=Exit name=Cancel><br></form>";
static char ConfigEditPage[] = "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">"
"<title></title></head><body>"
"<form style=\"font-family: monospace; text-align: center;\"method=post action=CFGSave?%s>"
"<textarea cols=100 rows=25 name=Msg>%s</textarea><br><br>"
"<input name=Save value=Save type=submit><input name=Cancel value=Cancel type=submit><br></form>";
static char EXCEPTMSG[80] = "";
VOID PollSession(struct HTTPConnectionInfo * Session)
{
int state, change;
int count, len;
char Msg[400] = "";
char Formatted[2048];
char * ptr1, * ptr2;
char c;
int Line;
// Poll Node
SessionState(Session->Stream, &state, &change);
if (change == 1)
{
int Line = Session->LastLine++;
free(Session->ScreenLines[Line]);
if (state == 1)// Connected
Session->ScreenLines[Line] = _strdup("*** Connected<br>\r\n");
else
Session->ScreenLines[Line] = _strdup("*** Disconnected<br>\r\n");
if (Line == 99)
Session->LastLine = 0;
Session->Changed = TRUE;
}
if (RXCount(Session->Stream) > 0)
{
do
{
GetMsg(Session->Stream, &Msg[0], &len, &count);
// replace cr with <br> and space with
ptr1 = Msg;
ptr2 = &Formatted[0];
if (Session->PartLine)
{
// Last line was incomplete - append to it
Line = Session->LastLine - 1;
if (Line < 0)
Line = 99;
strcpy(Formatted, Session->ScreenLines[Line]);
ptr2 += strlen(Formatted);
Session->LastLine = Line;
Session->PartLine = FALSE;
}
while (len--)
{
c = *(ptr1++);
if (c == 13)
{
int LineLen;
strcpy(ptr2, "<br>\r\n");
// Write to screen
Line = Session->LastLine++;
free(Session->ScreenLines[Line]);
LineLen = strlen(Formatted);
// if line starts with a colour code, process it
if (Formatted[0] == 0x1b && LineLen > 1)
{
int ColourCode = Formatted[1] - 10;
COLORREF Colour = Colours[ColourCode];
char ColString[30];
memmove(&Formatted[20], &Formatted[2], LineLen);
sprintf(ColString, "<font color=#%02X%02X%02X>", GetRValue(Colour), GetGValue(Colour), GetBValue(Colour));
memcpy(Formatted, ColString, 20);
strcat(Formatted, "</font>");
LineLen =+ 28;
}
Session->ScreenLineLen[Line] = LineLen;
Session->ScreenLines[Line] = _strdup(Formatted);
if (Line == 99)
Session->LastLine = 0;
ptr2 = &Formatted[0];
}
else if (c == 32)
{
memcpy(ptr2, " ", 6);
ptr2 += 6;
}
else if (c == '>')
{
memcpy(ptr2, ">", 4);
ptr2 += 4;
}
else if (c == '<')
{
memcpy(ptr2, "<", 4);
ptr2 += 4;
}
else
*(ptr2++) = c;
}
*ptr2 = 0;
if (ptr2 != &Formatted[0])
{
// Incomplete line
// Save to screen
Line = Session->LastLine++;
free(Session->ScreenLines[Line]);
Session->ScreenLines[Line] = _strdup(Formatted);
if (Line == 99)
Session->LastLine = 0;
Session->PartLine = TRUE;
}
// strcat(Session->ScreenBuffer, Formatted);
Session->Changed = TRUE;
} while (count > 0);
}
}
VOID HTTPTimer()
{
// Run every tick. Check for status change and data available
struct HTTPConnectionInfo * Session = SessionList; // active term mode sessions
struct HTTPConnectionInfo * PreviousSession = NULL;
while (Session)
{
Session->KillTimer++;
if (Session->Key[0] != 'T')
{
PreviousSession = Session;
Session = Session->Next;
continue;
}
if (Session->KillTimer > 3000) // Around 5 mins
{
int i;
int Stream = Session->Stream;
for (i = 0; i < 100; i++)
{
free(Session->ScreenLines[i]);
}
SessionControl(Stream, 2, 0);
SessionState(Stream, &i, &i);
DeallocateStream(Stream);
if (PreviousSession)
PreviousSession->Next = Session->Next; // Remove from chain
else
SessionList = Session->Next;
free(Session);
break;
}
PollSession(Session);
// if (Session->ResponseTimer == 0 && Session->Changed)
// Debugprintf("Data to send but no outstanding GET");
if (Session->ResponseTimer)
{
Session->ResponseTimer--;
if (Session->ResponseTimer == 0 || Session->Changed)
{
SOCKET sock = Session->sock;
char _REPLYBUFFER[100000];
int ReplyLen;
char Header[256];
int HeaderLen;
int Last = Session->LastLine;
int n;
strcpy(_REPLYBUFFER, TermOutput);
for (n = Last;;)
{
strcat(_REPLYBUFFER, Session->ScreenLines[n]);
if (n == 99)
n = -1;
if (++n == Last)
break;
}
ReplyLen = strlen(_REPLYBUFFER);
ReplyLen += sprintf(&_REPLYBUFFER[ReplyLen], "%s", TermOutputTail);
HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n", ReplyLen);
sendandcheck(sock, Header, HeaderLen);
sendandcheck(sock, _REPLYBUFFER, ReplyLen);
Session->ResponseTimer = Session->Changed = 0;
}
}
PreviousSession = Session;
Session = Session->Next;
}
}
struct HTTPConnectionInfo * AllocateSession(SOCKET sock, char Mode)
{
int KeyVal;
struct HTTPConnectionInfo * Session = zalloc(sizeof(struct HTTPConnectionInfo));
int i;
if (Session == NULL)
return NULL;
if (Mode == 'T')
{
// Terminal
for (i = 0; i < 20; i++)
Session->ScreenLines[i] = _strdup("Scroll to end<br>");
for (i = 20; i < 100; i++)
Session->ScreenLines[i] = _strdup("<br>\r\n");
Session->Stream = FindFreeStream();
if (Session->Stream == 0)
return NULL;
SessionControl(Session->Stream, 1, 0);
}
KeyVal = (int)sock * time(NULL);
sprintf(Session->Key, "%c%012X", Mode, KeyVal);
if (SessionList)
Session->Next = SessionList;
SessionList = Session;
return Session;
}
struct HTTPConnectionInfo * FindSession(char * Key)
{
struct HTTPConnectionInfo * Session = SessionList;
while (Session)
{
if (strcmp(Session->Key, Key) == 0)
return Session;
Session = Session->Next;
}
return NULL;
}
void ProcessTermInput(SOCKET sock, char * MsgPtr, int MsgLen, char * Key)
{
char _REPLYBUFFER[1024];
int ReplyLen = sprintf(_REPLYBUFFER, InputLine, Key, Key);
char Header[256];
int HeaderLen;
int State;
struct HTTPConnectionInfo * Session = FindSession(Key);
int Stream;
if (Session == NULL)
{
ReplyLen = sprintf(_REPLYBUFFER, "%s", LostSession);
}
else
{
char * input = strstr(MsgPtr, "\r\n\r\n"); // End of headers
char * end = &MsgPtr[MsgLen];
int Line = Session->LastLine++;
char * ptr1, * ptr2;
char c;
UCHAR hex;
Stream = Session->Stream;
input += 10;
ptr1 = ptr2 = input;
// Convert any %xx constructs
while (ptr1 != end)
{
c = *(ptr1++);
if (c == '%')
{
c = *(ptr1++);
if(isdigit(c))
hex = (c - '0') << 4;
else
hex = (tolower(c) - 'a' + 10) << 4;
c = *(ptr1++);
if(isdigit(c))
hex += (c - '0');
else
hex += (tolower(c) - 'a' + 10);
*(ptr2++) = hex;
}
else if (c == '+')
*(ptr2++) = 32;
else
*(ptr2++) = c;
}
end = ptr2;
*ptr2 = 0;
strcat(input, "<br>\r\n");
free(Session->ScreenLines[Line]);
Session->ScreenLines[Line] = _strdup(input);
if (Line == 99)
Session->LastLine = 0;
*end++ = 13;
*end = 0;
SessionStateNoAck(Stream, &State);
if (State == 0)
{
char AXCall[10];
SessionControl(Stream, 1, 0);
ConvToAX25(Session->HTTPCall, AXCall);
ChangeSessionCallsign(Stream, AXCall);
BPQHOSTVECTOR[Session->Stream -1].HOSTSESSION->Secure_Session = Session->USER->Secure;
}
SendMsg(Stream, input, end - input);
Session->Changed = TRUE;
Session->KillTimer = 0;
}
HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n", ReplyLen + strlen(Tail));
send(sock, Header, HeaderLen, 0);
send(sock, _REPLYBUFFER, ReplyLen, 0);
send(sock, Tail, strlen(Tail), 0);
}
void ProcessTermClose(SOCKET sock, char * MsgPtr, int MsgLen, char * Key)
{
char _REPLYBUFFER[8192];
int ReplyLen = sprintf(_REPLYBUFFER, InputLine, Key, Key);
char Header[256];
int HeaderLen;
struct HTTPConnectionInfo * Session = FindSession(Key);
if (Session)
{
Session->KillTimer = 99999;
}
ReplyLen = SetupNodeMenu(_REPLYBUFFER);
HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n"
"\r\n", ReplyLen + strlen(Tail));
send(sock, Header, HeaderLen, 0);
send(sock, _REPLYBUFFER, ReplyLen, 0);
send(sock, Tail, strlen(Tail), 0);
}
ProcessTermSignon(struct TNCINFO * TNC, SOCKET sock, char * MsgPtr, int MsgLen)
{
char _REPLYBUFFER[8192];
int ReplyLen;
char Header[256];
int HeaderLen;
char * input = strstr(MsgPtr, "\r\n\r\n"); // End of headers
char * user, * password, * Context, * Appl;
char NoApp[] = "";
struct TCPINFO * TCP = TNC->TCPInfo;
if (input)
{
int i;
struct UserRec * USER;
if (strstr(input, "Cancel=Cancel"))
{
ReplyLen = SetupNodeMenu(_REPLYBUFFER);
goto Sendit;
}
user = strtok_s(&input[9], "&", &Context);
password = strtok_s(NULL, "=", &Context);
password = strtok_s(NULL, "&", &Context);
Appl = strtok_s(NULL, "=", &Context);
Appl = strtok_s(NULL, "&", &Context);
if (Appl == 0)
Appl = NoApp;
for (i = 0; i < TCP->NumberofUsers; i++)
{
USER = TCP->UserRecPtr[i];
if (_stricmp(user, USER->UserName) == 0)
{
if (strcmp(password, USER->Password) == 0)
{
// ok
struct HTTPConnectionInfo * Session = AllocateSession(sock, 'T');
if (Session)
{
char AXCall[10];
ReplyLen = sprintf(_REPLYBUFFER, TermPage, Mycall, Mycall, Session->Key, Session->Key, Session->Key);
strcpy(Session->HTTPCall, USER->Callsign);
ConvToAX25(Session->HTTPCall, AXCall);
ChangeSessionCallsign(Session->Stream, AXCall);
BPQHOSTVECTOR[Session->Stream -1].HOSTSESSION->Secure_Session = USER->Secure;
Session->USER = USER;
if (Appl[0])
{
strcat(Appl, "\r");
SendMsg(Session->Stream, Appl, strlen(Appl));
}
}
else
{
ReplyLen = SetupNodeMenu(_REPLYBUFFER);
ReplyLen += sprintf(&_REPLYBUFFER[ReplyLen], "%s", BusyError);
}
break;
}
}
}
if (i == TCP->NumberofUsers)
{
// Not found
ReplyLen = sprintf(_REPLYBUFFER, TermSignon, Mycall, Mycall, Appl);
ReplyLen += sprintf(&_REPLYBUFFER[ReplyLen], "%s", PassError);
}
}
Sendit:
HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n", ReplyLen + strlen(Tail));
send(sock, Header, HeaderLen, 0);
send(sock, _REPLYBUFFER, ReplyLen, 0);
send(sock, Tail, strlen(Tail), 0);
return 1;
}
char * LookupKey(char * Key)
{
if (strcmp(Key, "##MY_CALLSIGN##") == 0)
{
char Mycall[10];
memcpy(Mycall, &MYNODECALL, 10);
strlop(Mycall, ' ');
return _strdup(Mycall);
}
return NULL;
}
int ProcessSpecialPage(char * Buffer, int FileSize)
{
// replaces ##xxx### constructs with the requested data
char * NewMessage = malloc(100000);
char * ptr1 = Buffer, * ptr2, * ptr3, * ptr4, * NewPtr = NewMessage;
int PrevLen;
int BytesLeft = FileSize;
int NewFileSize = FileSize;
char * StripPtr = ptr1;
// strip comments blocks
while (ptr4 = strstr(ptr1, "<!--"))
{
ptr2 = strstr(ptr4, "-->");
if (ptr2)
{
PrevLen = (ptr4 - ptr1);
memcpy(StripPtr, ptr1, PrevLen);
StripPtr += PrevLen;
ptr1 = ptr2 + 3;
BytesLeft = FileSize - (ptr1 - Buffer);
}
}
memcpy(StripPtr, ptr1, BytesLeft);
StripPtr += BytesLeft;
BytesLeft = StripPtr - Buffer;
FileSize = BytesLeft;
NewFileSize = FileSize;
ptr1 = Buffer;
ptr1[FileSize] = 0;
loop:
ptr2 = strstr(ptr1, "##");
if (ptr2)
{
PrevLen = (ptr2 - ptr1); // Bytes before special text
ptr3 = strstr(ptr2+2, "##");
if (ptr3)
{
char Key[80] = "";
int KeyLen;
char * NewText;
int NewTextLen;
ptr3 += 2;
KeyLen = ptr3 - ptr2;
if (KeyLen < 80)
memcpy(Key, ptr2, KeyLen);
NewText = LookupKey(Key);
if (NewText)
{
NewTextLen = strlen(NewText);
NewFileSize = NewFileSize + NewTextLen - KeyLen;
// NewMessage = realloc(NewMessage, NewFileSize);
memcpy(NewPtr, ptr1, PrevLen);
NewPtr += PrevLen;
memcpy(NewPtr, NewText, NewTextLen);
NewPtr += NewTextLen;
free(NewText);
NewText = NULL;
}
else
{
// Key not found, so just leave
memcpy(NewPtr, ptr1, PrevLen + KeyLen);
NewPtr += (PrevLen + KeyLen);
}
ptr1 = ptr3; // Continue scan from here
BytesLeft = Buffer + FileSize - ptr3;
}
else // Unmatched ##
{
memcpy(NewPtr, ptr1, PrevLen + 2);
NewPtr += (PrevLen + 2);
ptr1 = ptr2 + 2;
}
goto loop;
}
// Copy Rest
memcpy(NewPtr, ptr1, BytesLeft);
NewMessage[NewFileSize] = 0;
strcpy(Buffer, NewMessage);
free(NewMessage);
return NewFileSize;
}
int SendMessageFile(SOCKET sock, char * FN, BOOL OnlyifExists)
{
int FileSize, Sent, Loops = 0;
char * MsgBytes;
char MsgFile[512];
FILE * hFile;
int ReadLen;
BOOL Special = FALSE;
int Len;
int HeaderLen;
char Header[256];
time_t ctime;
char TimeString[64];
char FileTimeString[64];
struct stat STAT;
#ifdef WIN32
struct _EXCEPTION_POINTERS exinfo;
strcpy(EXCEPTMSG, "SendMessageFile");
__try {
#endif
if (strlen(FN) > 256)
{
FN[256] = 0;
Debugprintf("HTTP File Name too long %s", FN);
}
if (strcmp(FN, "/") == 0)
if (APRSActive)
sprintf(MsgFile, "%s/HTML/index.html", BPQDirectory);
else
sprintf(MsgFile, "%s/HTML/indexnoaprs.html", BPQDirectory);
else
sprintf(MsgFile, "%s/HTML%s", BPQDirectory, FN);
if (stat(MsgFile, &STAT) == -1)
{
if (OnlyifExists) // Set if we dont want an error response if missing
return -1;
Len = sprintf(Header, "HTTP/1.1 404 Not Found\r\nContent-Length: 16\r\n\r\nPage not found\r\n");
send(sock, Header, Len, 0);
return 0;
}
hFile = fopen(MsgFile, "rb");
if (hFile == 0)
{
if (OnlyifExists) // Set if we dont want an error response if missing
return -1;
Len = sprintf(Header, "HTTP/1.1 404 Not Found\r\nContent-Length: 16\r\n\r\nPage not found\r\n");
send(sock, Header, Len, 0);
return 0;
}
FileSize = STAT.st_size;
MsgBytes = malloc(FileSize + 1);
ReadLen = fread(MsgBytes, 1, FileSize, hFile);
fclose(hFile);
// ft.QuadPart -= 116444736000000000;
// ft.QuadPart /= 10000000;
// ctime = ft.LowPart;
FormatTime2(TimeString, STAT.st_ctime);
// if HTML file, look for ##...## substitutions
if ((strcmp(FN, "/") == 0 || strstr(FN, "htm" ) || strstr(FN, "HTM")) && strstr(MsgBytes, "##" ))
{
FileSize = ProcessSpecialPage(MsgBytes, FileSize);
ctime = time(NULL);
}
FormatTime2(FileTimeString, STAT.st_ctime);
FormatTime2(TimeString, time(NULL));
HeaderLen = sprintf(Header, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n"
"Content-Type: text/html\r\n"
"Date: %s\r\n"