Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpResponder.cpp « DuetNG « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 484b0d50322ebf5c5a83d69affade2feb82ab7a4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
/*
 * HttpResponder.cpp
 *
 *  Created on: 14 Apr 2017
 *      Author: David
 */

#include "HttpResponder.h"
#include "GCodes/GCodes.h"
#include "PrintMonitor.h"
#include "Libraries/General/IP4String.h"

#define KO_START "rr_"
const size_t KoFirst = 3;

const char* overflowResponse = "overflow";
const char* badEscapeResponse = "bad escape";

const uint32_t HttpReceiveTimeout = 2000;

HttpResponder::HttpResponder(NetworkResponder *n) : NetworkResponder(n)
{
}

// Ask the responder to accept this connection, returns true if it did
bool HttpResponder::Accept(Socket *s, Protocol protocol)
{
	if (responderState == ResponderState::free && protocol == HttpProtocol)
	{
		// Make sure we can get an output buffer before we accept the connection, or we won't be able to reply
		if (outBuf != nullptr || OutputBuffer::Allocate(outBuf))
		{
			responderState = ResponderState::reading;
			skt = s;
			timer = millis();

			// Reset the parse state variables
			clientPointer = 0;
			parseState = HttpParseState::doingCommandWord;
			numCommandWords = 0;
			numQualKeys = 0;
			numHeaderKeys = 0;
			commandWords[0] = clientMessage;

			if (reprap.Debug(moduleWebserver))
			{
				debugPrintf("HTTP connection accepted\n");
			}
			return true;
		}
		if (reprap.Debug(moduleWebserver))
		{
			debugPrintf("HTTP connection refused (no buffers)\n");
		}
	}
	return false;
}

// Do some work, returning true if we did anything significant
bool HttpResponder::Spin()
{
	switch (responderState)
	{
	case ResponderState::free:
		return false;

	case ResponderState::reading:
		{
			bool readSomething = false;
			for (;;)
			{
				char c;
				if (skt->ReadChar(c))
				{
					if (CharFromClient(c))
					{
						return true;
					}
					readSomething = true;
				}
				else
				{
					break;
				}
			}

			// Here when we were not able to read a character but we didn't receive a finished message
			if (readSomething)
			{
				timer = millis();			// restart the timeout
				return true;
			}

			if (!skt->CanRead() || millis() - timer >= HttpReceiveTimeout)
			{
				ConnectionLost();
				return true;
			}

			return false;
		}

	case ResponderState::gettingFileInfoLock:
		if (!fileInfoLock.Acquire(this))
		{
			return false;
		}
		responderState = ResponderState::gettingFileInfo;
		// no break

	case ResponderState::gettingFileInfo:
		if (SendFileInfo())
		{
			fileInfoLock.Release(this);					// release the lock
		}
		return true;

	case ResponderState::uploading:
		DoUpload();
		return true;

	case ResponderState::sending:
		SendData();
		return true;

	default:	// should not happen
		return false;
	}
}

// Process a character from the client
// Rewritten as a state machine by dc42 to increase capability and speed, and reduce RAM requirement.
// On entry:
//  There is space for at least 1 character in clientMessage.
// On return:
//	If we return false:
//		We want more characters. There is space for at least 1 character in clientMessage.
//	If we return true:
//		We have processed the message and sent the reply. No more characters may be read from this message.
// Whenever this calls ProcessMessage:
//	The first line has been split up into words. Variables numCommandWords and commandWords give the number of words we found
//  and the pointers to each word. The second word is treated specially. It is assumed to be a filename followed by an optional
//  qualifier comprising key/value pairs. Both may include %xx escapes, and the qualifier may include + to mean space. We store
//  a pointer to the filename without qualifier in commandWords[1]. We store the qualifier key/value pointers in array 'qualifiers'
//  and the number of them in numQualKeys.
//  The remaining lines have been parsed as header name/value pairs. Pointers to them are stored in array 'headers' and the number
//  of them in numHeaders.
// If one of our arrays is about to overflow, or the message is not in a format we expect, then we call RejectMessage with an
// appropriate error code and string.
bool HttpResponder::CharFromClient(char c)
{
	switch (parseState)
	{
	case HttpParseState::doingCommandWord:
		switch(c)
		{
		case '\n':
			clientMessage[clientPointer++] = 0;
			++numCommandWords;
			numHeaderKeys = 0;
			headers[0].key = clientMessage + clientPointer;
			parseState = HttpParseState::doingHeaderKey;
			break;
		case '\r':
			break;
		case ' ':
		case '\t':
			clientMessage[clientPointer++] = 0;
			if (numCommandWords < MaxCommandWords)
			{
				++numCommandWords;
				commandWords[numCommandWords] = clientMessage + clientPointer;
				if (numCommandWords == 1)
				{
					parseState = HttpParseState::doingFilename;
				}
			}
			else
			{
				RejectMessage("too many command words");
				return true;
			}
			break;
		default:
			clientMessage[clientPointer++] = c;
			break;
		}
		break;

	case HttpParseState::doingFilename:
		switch(c)
		{
		case '\n':
			clientMessage[clientPointer++] = 0;
			++numCommandWords;
			numQualKeys = 0;
			numHeaderKeys = 0;
			headers[0].key = clientMessage + clientPointer;
			parseState = HttpParseState::doingHeaderKey;
			break;
		case '?':
			clientMessage[clientPointer++] = 0;
			++numCommandWords;
			numQualKeys = 0;
			qualifiers[0].key = clientMessage + clientPointer;
			parseState = HttpParseState::doingQualifierKey;
			break;
		case '%':
			parseState = HttpParseState::doingFilenameEsc1;
			break;
		case '\r':
			break;
		case ' ':
		case '\t':
			clientMessage[clientPointer++] = 0;
			if (numCommandWords < MaxCommandWords)
			{
				++numCommandWords;
				commandWords[numCommandWords] = clientMessage + clientPointer;
				parseState = HttpParseState::doingCommandWord;
			}
			else
			{
				RejectMessage("too many command words");
				return true;
			}
			break;
		default:
			clientMessage[clientPointer++] = c;
			break;
		}
		break;

	case HttpParseState::doingQualifierKey:
		switch(c)
		{
		case '=':
			clientMessage[clientPointer++] = 0;
			qualifiers[numQualKeys].value = clientMessage + clientPointer;
			++numQualKeys;
			parseState = HttpParseState::doingQualifierValue;
			break;
		case '\n':	// key with no value
		case ' ':
		case '\t':
		case '\r':
		case '%':	// none of our keys needs escaping, so treat an escape within a key as an error
		case '&':	// key with no value
			RejectMessage("bad qualifier key");
			return true;
		default:
			clientMessage[clientPointer++] = c;
			break;
		}
		break;

	case HttpParseState::doingQualifierValue:
		switch(c)
		{
		case '\n':
			clientMessage[clientPointer++] = 0;
			qualifiers[numQualKeys].key = clientMessage + clientPointer;	// so that we can read the whole value even if it contains a null
			numHeaderKeys = 0;
			headers[0].key = clientMessage + clientPointer;
			parseState = HttpParseState::doingHeaderKey;
			break;
		case ' ':
		case '\t':
			clientMessage[clientPointer++] = 0;
			qualifiers[numQualKeys].key = clientMessage + clientPointer;	// so that we can read the whole value even if it contains a null
			commandWords[numCommandWords] = clientMessage + clientPointer;
			parseState = HttpParseState::doingCommandWord;
			break;
		case '\r':
			break;
		case '%':
			parseState = HttpParseState::doingQualifierValueEsc1;
			break;
		case '&':
			// Another variable is coming
			clientMessage[clientPointer++] = 0;
			qualifiers[numQualKeys].key = clientMessage + clientPointer;	// so that we can read the whole value even if it contains a null
			if (numQualKeys < MaxQualKeys)
			{
				parseState = HttpParseState::doingQualifierKey;
			}
			else
			{
				RejectMessage("too many keys in qualifier");
				return true;
			}
			break;
		case '+':
			clientMessage[clientPointer++] = ' ';
			break;
		default:
			clientMessage[clientPointer++] = c;
			break;
		}
		break;

	case HttpParseState::doingFilenameEsc1:
	case HttpParseState::doingQualifierValueEsc1:
		if (c >= '0' && c <= '9')
		{
			decodeChar = (c - '0') << 4;
			parseState = (HttpParseState)((int)parseState + 1);
		}
		else if (c >= 'A' && c <= 'F')
		{
			decodeChar = (c - ('A' - 10)) << 4;
			parseState = (HttpParseState)((int)parseState + 1);
		}
		else
		{
			RejectMessage(badEscapeResponse);
			return true;
		}
		break;

	case HttpParseState::doingFilenameEsc2:
	case HttpParseState::doingQualifierValueEsc2:
		if (c >= '0' && c <= '9')
		{
			clientMessage[clientPointer++] = decodeChar | (c - '0');
			parseState = (HttpParseState)((int)parseState - 2);
		}
		else if (c >= 'A' && c <= 'F')
		{
			clientMessage[clientPointer++] = decodeChar | (c - ('A' - 10));
			parseState = (HttpParseState)((int)parseState - 2);
		}
		else
		{
			RejectMessage(badEscapeResponse);
			return true;
		}
		break;

	case HttpParseState::doingHeaderKey:
		switch(c)
		{
		case '\n':
			if (clientMessage + clientPointer == headers[numHeaderKeys].key)	// if the key hasn't started yet, then this is the blank line at the end
			{
				ProcessMessage();
				return true;
			}
			else
			{
				RejectMessage("unexpected newline");
				return true;
			}
			break;
		case '\r':
			break;
		case ':':
			if (numHeaderKeys == MaxHeaders - 1)
			{
				RejectMessage("too many header key-value pairs");
				return true;
			}
			clientMessage[clientPointer++] = 0;
			headers[numHeaderKeys].value = clientMessage + clientPointer;
			++numHeaderKeys;
			parseState = HttpParseState::expectingHeaderValue;
			break;
		default:
			clientMessage[clientPointer++] = c;
			break;
		}
		break;

	case HttpParseState::expectingHeaderValue:
		if (c == ' ' || c == '\t')
		{
			break;		// ignore spaces between header key and value
		}
		parseState = HttpParseState::doingHeaderValue;
		// no break

	case HttpParseState::doingHeaderValue:
		if (c == '\n')
		{
			parseState = HttpParseState::doingHeaderContinuation;
		}
		else if (c != '\r')
		{
			clientMessage[clientPointer++] = c;
		}
		break;

	case HttpParseState::doingHeaderContinuation:
		switch(c)
		{
		case ' ':
		case '\t':
			// It's a continuation of the previous value
			clientMessage[clientPointer++] = c;
			parseState = HttpParseState::doingHeaderValue;
			break;
		case '\n':
			// It's the blank line
			clientMessage[clientPointer] = 0;
			ProcessMessage();
			return true;
		case '\r':
			break;
		default:
			// It's a new key
			if (clientPointer + 3 <= ARRAY_SIZE(clientMessage))
			{
				clientMessage[clientPointer++] = 0;
				headers[numHeaderKeys].key = clientMessage + clientPointer;
				clientMessage[clientPointer++] = c;
				parseState = HttpParseState::doingHeaderKey;
			}
			else
			{
				RejectMessage(overflowResponse);
				return true;
			}
			break;
		}
		break;

	default:
		break;
	}

	if (clientPointer == ARRAY_SIZE(clientMessage))
	{
		RejectMessage(overflowResponse);
		return true;
	}
	return false;
}

// Get the Json response for this command.
// 'value' is null-terminated, but we also pass its length in case it contains embedded nulls, which matters when uploading files.
// Return true if we generated a json response to send, false if we didn't and changed the state instead
bool HttpResponder::GetJsonResponse(const char* request, OutputBuffer *&response, bool& keepOpen)
{
	keepOpen = false;	// assume we don't want to persist the connection
	if (StringEquals(request, "connect") && GetKeyValue("password") != nullptr)
	{
		if (!CheckAuthenticated())
		{
			if (!reprap.CheckPassword(GetKeyValue("password")))
			{
				// Wrong password
				response->copy("{\"err\":1}");
				reprap.GetPlatform().MessageF(LogMessage, "HTTP client %s attempted login with incorrect password\n", IP4String(GetRemoteIP()).c_str());
				return true;
			}
			if (!Authenticate())
			{
				// No more HTTP sessions available
				response->copy("{\"err\":2}");
				reprap.GetPlatform().MessageF(LogMessage, "HTTP client %s attempted login but no more sessions available\n", IP4String(GetRemoteIP()).c_str());
				return true;
			}
		}

		// See if we can update the current RTC date and time
		if (numQualKeys > 1 && StringEquals(qualifiers[1].key, "time") && !GetPlatform().IsDateTimeSet())
		{
			struct tm timeInfo;
			memset(&timeInfo, 0, sizeof(timeInfo));
			if (strptime(qualifiers[1].value, "%Y-%m-%dT%H:%M:%S", &timeInfo) != nullptr)
			{
				time_t newTime = mktime(&timeInfo);
				GetPlatform().SetDateTime(newTime);
			}
		}

		// Client has been logged in
		response->printf("{\"err\":0,\"sessionTimeout\":%" PRIu32 ",\"boardType\":\"%s\"}", HttpSessionTimeout, GetPlatform().GetBoardString());
		reprap.GetPlatform().MessageF(LogMessage, "HTTP client %s login succeeded\n", IP4String(GetRemoteIP()).c_str());
	}
	else if (!CheckAuthenticated())
	{
		RejectMessage("Not authorized", 500);
		return false;
	}
	else if (StringEquals(request, "disconnect"))
	{
		response->printf("{\"err\":%d}", (RemoveAuthentication()) ? 0 : 1);
		reprap.GetPlatform().MessageF(LogMessage, "HTTP client %s disconnected\n", IP4String(GetRemoteIP()).c_str());
	}
	else if (StringEquals(request, "status"))
	{
		int type = 0;
		if (GetKeyValue("type") != nullptr)
		{
			// New-style JSON status responses
			type = atoi(GetKeyValue("type"));
			if (type < 1 || type > 3)
			{
				type = 1;
			}

			OutputBuffer::Release(response);
			response = reprap.GetStatusResponse(type, ResponseSource::HTTP);
		}
		else
		{
			// Deprecated
			OutputBuffer::Release(response);
			response = reprap.GetLegacyStatusResponse(1, 0);
		}
	}
	else if (StringEquals(request, "gcode") && GetKeyValue("gcode") != nullptr)
	{
		RegularGCodeInput * const httpInput = reprap.GetGCodes().GetHTTPInput();
		httpInput->Put(HttpMessage, GetKeyValue("gcode"));
		response->printf("{\"buff\":%u}", httpInput->BufferSpaceLeft());
	}
	else if (StringEquals(request, "upload"))
	{
		response->printf("{\"err\":%d}", (uploadError) ? 1 : 0);
	}
	else if (StringEquals(request, "delete") && GetKeyValue("name") != nullptr)
	{
		bool ok = GetPlatform().GetMassStorage()->Delete(FS_PREFIX, GetKeyValue("name"));
		response->printf("{\"err\":%d}", (ok) ? 0 : 1);
	}
	else if (StringEquals(request, "filelist") && GetKeyValue("dir") != nullptr)
	{
		OutputBuffer::Release(response);
		response = reprap.GetFilelistResponse(GetKeyValue("dir"));
	}
	else if (StringEquals(request, "files"))
	{
		const char* dir = GetKeyValue("dir");
		if (dir == nullptr)
		{
			dir = GetPlatform().GetGCodeDir();
		}
		const char* const flagDirsVal = GetKeyValue("flagDirs");
		const bool flagDirs = flagDirsVal != nullptr && atoi(flagDirsVal) == 1;
		OutputBuffer::Release(response);
		response = reprap.GetFilesResponse(dir, flagDirs);
	}
	else if (StringEquals(request, "fileinfo"))
	{
		const char* const nameVal = GetKeyValue("name");
		if (nameVal != nullptr)
		{
			// Regular rr_fileinfo?name=xxx call
			SafeStrncpy(filenameBeingProcessed, nameVal, ARRAY_SIZE(filenameBeingProcessed));
		}
		else
		{
			// Simple rr_fileinfo call to get info about the file being printed
			filenameBeingProcessed[0] = 0;
		}
		responderState = ResponderState::gettingFileInfoLock;
		return false;
	}
	else if (StringEquals(request, "move"))
	{
		const char* const oldVal = GetKeyValue("old");
		const char* const newVal = GetKeyValue("new");
		bool success = false;
		if (oldVal != nullptr && newVal != nullptr)
		{
			success = GetPlatform().GetMassStorage()->Rename(oldVal, newVal);
		}
		response->printf("{\"err\":%d}", (success) ? 0 : 1);
	}
	else if (StringEquals(request, "mkdir"))
	{
		const char* dirVal = GetKeyValue("dir");
		bool success = false;
		if (dirVal != nullptr)
		{
			success = (GetPlatform().GetMassStorage()->MakeDirectory(dirVal));
		}
		response->printf("{\"err\":%d}", (success) ? 0 : 1);
	}
	else if (StringEquals(request, "config"))
	{
		OutputBuffer::Release(response);
		response = reprap.GetConfigResponse();
	}
	else
	{
		RejectMessage("Unknown request", 500);
		return false;
	}
	return true;
}

const char* HttpResponder::GetKeyValue(const char *key) const
{
	for (size_t i = 0; i < numQualKeys; ++i)
	{
		if (StringEquals(qualifiers[i].key, key))
		{
			return qualifiers[i].value;
		}
	}
	return nullptr;
}

// Called to process a FileInfo request, which may take several calls
// When we have finished, set the state back to free.
bool HttpResponder::SendFileInfo()
{
	OutputBuffer *jsonResponse = nullptr;
	const bool gotFileInfo = reprap.GetPrintMonitor().GetFileInfoResponse(filenameBeingProcessed, jsonResponse);
	if (gotFileInfo)
	{
		// Got it - send the response now
		outBuf->copy(	"HTTP/1.1 200 OK\n"
						"Cache-Control: no-cache, no-store, must-revalidate\n"
						"Pragma: no-cache\n"
						"Expires: 0\n"
						"Access-Control-Allow-Origin: *\n"
						"Content-Type: application/json\n"
					);
		outBuf->catf("Content-Length: %u\n", (jsonResponse != nullptr) ? jsonResponse->Length() : 0);
		outBuf->cat("Connection: close\n\n");
		outBuf->Append(jsonResponse);
		Commit();
	}
	return gotFileInfo;
}

// Authenticate current IP and return true on success
bool HttpResponder::Authenticate()
{
	if (CheckAuthenticated())
	{
		return true;
	}

	if (numSessions < MaxHttpSessions)
	{
		sessions[numSessions].ip = GetRemoteIP();
		sessions[numSessions].lastQueryTime = millis();
		sessions[numSessions].isPostUploading = false;
		numSessions++;
		return true;
	}
	return false;
}

// Check and update the authentication
bool HttpResponder::CheckAuthenticated()
{
	const uint32_t remoteIP = GetRemoteIP();
	for (size_t i = 0; i < numSessions; i++)
	{
		if (sessions[i].ip == remoteIP)
		{
			sessions[i].lastQueryTime = millis();
			return true;
		}
	}
	return false;
}

bool HttpResponder::RemoveAuthentication()
{
	const uint32_t remoteIP = skt->GetRemoteIP();
	for (size_t i = numSessions; i != 0; )
	{
		--i;
		if (sessions[i].ip == remoteIP)
		{
			if (sessions[i].isPostUploading)
			{
				// Don't allow sessions with active POST uploads to be removed
				return false;
			}

			for (size_t k = i + 1; k < numSessions; ++k)
			{
				memcpy(&sessions[k - 1], &sessions[k], sizeof(HttpSession));
			}
			numSessions--;
			return true;
		}
	}
	return false;
}

void HttpResponder::SendFile(const char* nameOfFileToSend, bool isWebFile)
{
	FileStore *fileToSend = nullptr;
	bool zip = false;

	if (isWebFile)
	{
		if (nameOfFileToSend[0] == '/')
		{
			++nameOfFileToSend;						// all web files are relative to the /www folder, so remove the leading '/'
			if (nameOfFileToSend[0] == 0)
			{
				nameOfFileToSend = INDEX_PAGE_FILE;
			}
		}

		// Try to open a gzipped version of the file first
		if (!StringEndsWith(nameOfFileToSend, ".gz") && strlen(nameOfFileToSend) + 3 <= FILENAME_LENGTH)
		{
			char nameBuf[FILENAME_LENGTH + 1];
			strcpy(nameBuf, nameOfFileToSend);
			strcat(nameBuf, ".gz");
			fileToSend = GetPlatform().GetFileStore(GetPlatform().GetWebDir(), nameBuf, OpenMode::read);
			if (fileToSend != nullptr)
			{
				zip = true;
			}
		}

		// If that failed, try to open the normal version of the file
		if (fileToSend == nullptr)
		{
			fileToSend = GetPlatform().GetFileStore(GetPlatform().GetWebDir(), nameOfFileToSend, OpenMode::read);
		}

		// If we still couldn't find the file and it was an HTML file, return the 404 error page
		if (fileToSend == nullptr && (StringEndsWith(nameOfFileToSend, ".html") || StringEndsWith(nameOfFileToSend, ".htm")))
		{
			nameOfFileToSend = FOUR04_PAGE_FILE;
			fileToSend = GetPlatform().GetFileStore(GetPlatform().GetWebDir(), nameOfFileToSend, OpenMode::read);
		}

		if (fileToSend == nullptr)
		{
			RejectMessage("not found", 404);
			return;
		}
		fileBeingSent = fileToSend;
	}
	else
	{
		fileToSend = GetPlatform().GetFileStore(FS_PREFIX, nameOfFileToSend, OpenMode::read);
		if (fileToSend == nullptr)
		{
			RejectMessage("not found", 404);
			return;
		}
		fileBeingSent = fileToSend;
	}

	outBuf->copy("HTTP/1.1 200 OK\n");

	// Don't cache files served by rr_download
	if (!isWebFile)
	{
		outBuf->cat(	"Cache-Control: no-cache, no-store, must-revalidate\n"
						"Pragma: no-cache\n"
						"Expires: 0\n"
						"Access-Control-Allow-Origin: *\n"
					);
	}

	const char* contentType;
	if (StringEndsWith(nameOfFileToSend, ".png"))
	{
		contentType = "image/png";
	}
	else if (StringEndsWith(nameOfFileToSend, ".ico"))
	{
		contentType = "image/x-icon";
	}
	else if (StringEndsWith(nameOfFileToSend, ".js"))
	{
		contentType = "application/javascript";
	}
	else if (StringEndsWith(nameOfFileToSend, ".css"))
	{
		contentType = "text/css";
	}
	else if (StringEndsWith(nameOfFileToSend, ".htm") || StringEndsWith(nameOfFileToSend, ".html"))
	{
		contentType = "text/html";
	}
	else if (StringEndsWith(nameOfFileToSend, ".zip"))
	{
		contentType = "application/zip";
		zip = true;
	}
	else if (StringEndsWith(nameOfFileToSend, ".g") || StringEndsWith(nameOfFileToSend, ".gc") || StringEndsWith(nameOfFileToSend, ".gcode"))
	{
		contentType = "text/plain";
	}
	else
	{
		contentType = "application/octet-stream";
	}
	outBuf->catf("Content-Type: %s\n", contentType);

	if (zip && fileToSend != nullptr)
	{
		outBuf->cat("Content-Encoding: gzip\n");
		outBuf->catf("Content-Length: %lu\n", fileToSend->Length());
	}

	outBuf->cat("Connection: close\n\n");
	Commit();
}

void HttpResponder::SendGCodeReply()
{
	// Do we need to keep the G-Code reply for other clients?
	bool clearReply = false;
	if (!gcodeReply->IsEmpty())
	{
		clientsServed++;
		if (clientsServed < numSessions)
		{
			// Yes - make sure the Network class doesn't discard its buffers yet
			// NB: This must happen here, because NetworkTransaction::Write() might already release OutputBuffers
			gcodeReply->IncreaseReferences(1);
		}
		else
		{
			// No - clean up again later
			clearReply = true;
		}

		if (reprap.Debug(moduleWebserver))
		{
			GetPlatform().MessageF(UsbMessage, "Sending G-Code reply to client %d of %d (length %u)\n", clientsServed, numSessions, gcodeReply->DataLength());
		}
	}

	// Send the whole G-Code reply as plain text to the client
	outBuf->copy(	"HTTP/1.1 200 OK\n"
					"Cache-Control: no-cache, no-store, must-revalidate\n"
					"Pragma: no-cache\n"
					"Expires: 0\n"
					"Access-Control-Allow-Origin: *\n"
					"Content-Type: text/plain\n"
				);
	outBuf->catf("Content-Length: %u\n", gcodeReply->DataLength());
	outBuf->cat("Connection: close\n\n");
	outStack->Append(gcodeReply);
	Commit();

	// Possibly clean up the G-code reply once again
	if (clearReply)
	{
		gcodeReply->Clear();
	}
}

void HttpResponder::SendJsonResponse(const char* command)
{
	// Try to authorise the user automatically to retain compatibility with the old web interface
	if (!CheckAuthenticated() && reprap.NoPasswordSet())
	{
		Authenticate();
	}

	// Update the authentication status and try handle "text/plain" requests here
	if (CheckAuthenticated())
	{
		if (StringEquals(command, "reply"))			// rr_reply
		{
			SendGCodeReply();
			return;
		}

		if (StringEquals(command, "configfile"))	// rr_configfile [DEPRECATED]
		{
			const char *configPath = GetPlatform().GetMassStorage()->CombineName(GetPlatform().GetSysDir(), GetPlatform().GetConfigFile());
			char fileName[FILENAME_LENGTH];
			SafeStrncpy(fileName, configPath, ARRAY_SIZE(fileName));
			SendFile(fileName, false);
			return;
		}

		if (StringEquals(command, "download") && StringEquals(qualifiers[0].key, "name"))
		{
			SendFile(qualifiers[0].value, false);
			return;
		}
	}

	// Try to process a request for JSON responses
	OutputBuffer *jsonResponse;
	if (!OutputBuffer::Allocate(jsonResponse))
	{
		// Reset the connection immediately if we cannot write any data. Should never happen.
		skt->Terminate();
		return;
	}

	bool mayKeepOpen;
	const bool gotResponse = GetJsonResponse(command, jsonResponse, mayKeepOpen);
	if (!gotResponse)
	{
		// Either this request was rejected, or it will take longer to process e.g. rr_fileinfo
		OutputBuffer::Release(jsonResponse);
		return;
	}

	// Send the JSON response
	bool keepOpen = false;
	if (mayKeepOpen)
	{
		// Check that the browser wants to persist the connection too
		for (size_t i = 0; i < numHeaderKeys; ++i)
		{
			if (StringEquals(headers[i].key, "Connection"))
			{
				// Comment out the following line to disable persistent connections
				keepOpen = StringEquals(headers[i].value, "keep-alive");
				break;
			}
		}
	}

	outBuf->copy(	"HTTP/1.1 200 OK\n"
					"Cache-Control: no-cache, no-store, must-revalidate\n"
					"Pragma: no-cache\n"
					"Expires: 0\n"
					"Access-Control-Allow-Origin: *\n"
					"Content-Type: application/json\n"
				);
	outBuf->catf("Content-Length: %u\n", (jsonResponse != nullptr) ? jsonResponse->Length() : 0);
	outBuf->catf("Connection: %s\n\n", keepOpen ? "keep-alive" : "close");
	outBuf->Append(jsonResponse);

	Commit(keepOpen ? ResponderState::reading : ResponderState::free);
}

// Process the message received so far. We have reached the end of the headers.
// Return true if the message is complete, false if we want to continue receiving data (i.e. postdata)
void HttpResponder::ProcessMessage()
{
	if (reprap.Debug(moduleWebserver))
	{
		GetPlatform().MessageF(UsbMessage, "HTTP req, command words {");
		for (size_t i = 0; i < numCommandWords; ++i)
		{
			GetPlatform().MessageF(UsbMessage, " %s", commandWords[i]);
		}
		GetPlatform().Message(UsbMessage, " }, parameters {");

		for (size_t i = 0; i < numQualKeys; ++i)
		{
			GetPlatform().MessageF(UsbMessage, " %s=%s", qualifiers[i].key, qualifiers[i].value);
		}
		GetPlatform().Message(UsbMessage, " }\n");
	}

	if (numCommandWords < 2)
	{
		RejectMessage("too few command words");
		return;
	}

	if (StringEquals(commandWords[0], "GET"))
	{
		if (StringStartsWith(commandWords[1], KO_START))
		{
			SendJsonResponse(commandWords[1] + KoFirst);
		}
		else if (commandWords[1][0] == '/' && StringStartsWith(commandWords[1] + 1, KO_START))
		{
			SendJsonResponse(commandWords[1] + 1 + KoFirst);
		}
		else
		{
			SendFile(commandWords[1], true);
		}
		return;
	}

	if (StringEquals(commandWords[0], "OPTIONS"))
	{
		outBuf->copy(	"HTTP/1.1 200 OK\n"
						"Allow: OPTIONS, GET, POST\n"
						"Cache-Control: no-cache, no-store, must-revalidate\n"
						"Pragma: no-cache\n"
						"Expires: 0\n"
						"Access-Control-Allow-Origin: *\n"
						"Access-Control-Allow-Headers: Content-Type\n"
						"Content-Length: 0\n"
						"\n"
					);
		Commit();
		return;
	}

	if (CheckAuthenticated() && StringEquals(commandWords[0], "POST"))
	{
		const bool isUploadRequest = (StringEquals(commandWords[1], KO_START "upload"))
								  || (commandWords[1][0] == '/' && StringEquals(commandWords[1] + 1, KO_START "upload"));
		if (isUploadRequest)
		{
			if (numQualKeys > 0 && StringEquals(qualifiers[0].key, "name"))
			{
				// See how many bytes we expect to read
				bool contentLengthFound = false;
				for (size_t i = 0; i < numHeaderKeys; i++)
				{
					if (StringEquals(headers[i].key, "Content-Length"))
					{
						postFileLength = atoi(headers[i].value);
						contentLengthFound = true;
						break;
					}
				}

				// Start POST file upload
				if (!contentLengthFound)
				{
					RejectMessage("invalid POST upload request");
					return;
				}

				// Start a new file upload
				FileStore *file = GetPlatform().GetFileStore(FS_PREFIX, qualifiers[0].value, OpenMode::write);
				if (file == nullptr)
				{
					RejectMessage("could not create file");
					return;

				}
				StartUpload(file, qualifiers[0].value);

				// Try to get the last modified file date and time
				if (numQualKeys > 1 && StringEquals(qualifiers[1].key, "time"))
				{
					struct tm timeInfo;
					memset(&timeInfo, 0, sizeof(timeInfo));
					if (strptime(qualifiers[1].value, "%Y-%m-%dT%H:%M:%S", &timeInfo) != nullptr)
					{
						fileLastModified  = mktime(&timeInfo);
					}
					else
					{
						fileLastModified = 0;
					}
				}
				else
				{
					fileLastModified = 0;
				}

				if (reprap.Debug(moduleWebserver))
				{
					GetPlatform().MessageF(UsbMessage, "Start uploading file %s length %lu\n", qualifiers[0].value, postFileLength);
				}
				uploadedBytes = 0;

				// Keep track of the connection that is now uploading
				const uint32_t remoteIP = GetRemoteIP();
				const uint16_t remotePort = skt->GetRemotePort();
				for(size_t i = 0; i < numSessions; i++)
				{
					if (sessions[i].ip == remoteIP)
					{
						sessions[i].postPort = remotePort;
						sessions[i].isPostUploading = true;
						break;
					}
				}
				return;
			}
		}
		RejectMessage("only rr_upload is supported for POST requests");
	}
	else
	{
		RejectMessage("Unknown message type or not authenticated");
	}
}

// Reject the current message
void HttpResponder::RejectMessage(const char* response, unsigned int code)
{
	if (reprap.Debug(moduleWebserver))
	{
		GetPlatform().MessageF(UsbMessage, "Webserver: rejecting message with: %u %s\n", code, response);
	}
	outBuf->printf("HTTP/1.1 %u %s\nConnection: close\n\n", code, response);
	Commit();
}

// This function overrides the one in class NetworkResponder.
// It tries to process a chunk of uploaded data and changes the state if finished.
void HttpResponder::DoUpload()
{
	const uint8_t *buffer;
	size_t len;
	if (skt->ReadBuffer(buffer, len))
	{
		skt->Taken(len);
		uploadedBytes += len;

		if (!fileBeingUploaded.Write(buffer, len))
		{
			uploadError = true;
			GetPlatform().Message(ErrorMessage, "Could not write upload data!\n");
			CancelUpload();
			SendJsonResponse("upload");
			return;
		}
	}

	// See if the upload has finished
	if (uploadedBytes >= postFileLength)
	{
		// Reset POST upload state for this client
		const uint32_t remoteIP = GetRemoteIP();
		for (size_t i = 0; i < numSessions; i++)
		{
			if (sessions[i].ip == remoteIP && sessions[i].isPostUploading)
			{
				sessions[i].isPostUploading = false;
				sessions[i].lastQueryTime = millis();
				break;
			}
		}

		FinishUpload(postFileLength, fileLastModified);
		SendJsonResponse("upload");
		return;
	}
	else if (!skt->CanRead())
	{
		// We cannot read any more, discard the transaction
		ConnectionLost();
	}
}

// This is called to force termination if we implement the specified protocol
void HttpResponder::Terminate(Protocol protocol)
{
	if (responderState != ResponderState::free && (protocol == HttpProtocol || protocol == AnyProtocol))
	{
		ConnectionLost();
	}
}

// This overrides the version in class NetworkResponder
void HttpResponder::ConnectionLost()
{
	fileInfoLock.Release(this);
	NetworkResponder::ConnectionLost();
}

// This overrides the version in class NetworkResponder
void HttpResponder::CancelUpload()
{
	if (skt != nullptr)
	{
		for (size_t i = 0; i < numSessions; i++)
		{
			if (sessions[i].ip == skt->GetRemoteIP() && sessions[i].isPostUploading)
			{
				sessions[i].isPostUploading = false;
				sessions[i].lastQueryTime = millis();
				break;
			}
		}
	}
	NetworkResponder::CancelUpload();
}

void HttpResponder::Diagnostics(MessageType mt) const
{
	GetPlatform().MessageF(mt, " HTTP(%d)", (int)responderState);
}

/*static*/ void HttpResponder::HandleGCodeReply(const char *reply)
{
	if (numSessions > 0)
	{
		OutputBuffer *buffer = gcodeReply->GetLastItem();
		if (buffer == nullptr || buffer->IsReferenced())
		{
			if (!OutputBuffer::Allocate(buffer))
			{
				// No more space available, stop here
				return;
			}
			gcodeReply->Push(buffer);
		}

		buffer->cat(reply);
		clientsServed = 0;
		seq++;
	}
}

/*static*/ void HttpResponder::HandleGCodeReply(OutputBuffer *reply)
{
	if (reply != nullptr)
	{
		if (numSessions > 0)
		{
			// FIXME: This might cause G-code responses to be sent twice to fast HTTP clients, but
			// I (chrishamm) cannot think of a nicer way to deal with slow clients at the moment...
			gcodeReply->Push(reply);
			clientsServed = 0;
			seq++;
		}
		else
		{
			// Don't use buffers that may never get released...
			OutputBuffer::ReleaseAll(reply);
		}
	}
}


/*static*/ void HttpResponder::CheckSessions()
{
	const uint32_t now = millis();
	for (size_t i = numSessions; i != 0; )
	{
		--i;
		if ((now - sessions[i].lastQueryTime) > HttpSessionTimeout)
		{
			// Check for timed out sessions
			for (size_t k = i + 1; k < numSessions; k++)
			{
				memcpy(&sessions[k - 1], &sessions[k], sizeof(HttpSession));
			}
			numSessions--;
			clientsServed++;	// assume the disconnected client hasn't fetched the G-Code reply yet
		}
	}

	// If we cannot send the G-Code reply to anyone, we may free up some run-time space by dumping it
	if (numSessions == 0 || clientsServed >= numSessions)
	{
		while (!gcodeReply->IsEmpty())
		{
			OutputBuffer::ReleaseAll(gcodeReply->Pop());
		}
		clientsServed = 0;
	}
}

/*static*/ void HttpResponder::CommonDiagnostics(MessageType mtype)
{
	GetPlatform().MessageF(mtype, "HTTP sessions: %u of %u\n", numSessions, MaxHttpSessions);
}

// Static data

HttpResponder::HttpSession HttpResponder::sessions[MaxHttpSessions];
unsigned int HttpResponder::numSessions = 0;
unsigned int HttpResponder::clientsServed = 0;

uint32_t HttpResponder::seq = 0;
OutputStack *HttpResponder::gcodeReply = new OutputStack();

NetworkResponderLock HttpResponder::fileInfoLock;

// End