@@ -47,10 +47,10 @@ def elaborate(self, _platform):
4747 parser_demux = m .submodules .parser_demux = StreamDemux (mux_width = 4 , stream_width = 8 )
4848 connect (m , self .session .inbound .data , parser_demux .input )
4949
50- # TODO: #4 - Add packet count and RFC2324 endpoints
5150 MATCHED_LED_PATH = 1 # start_matcher path match is in the order the paths are connected.
5251 MATCHED_COUNT_PATH = 2
53- start_matcher = m .submodules .start_matcher = ParseStart (["/led" , "/count" ])
52+ MATCHED_COFFEE_PATH = 3
53+ start_matcher = m .submodules .start_matcher = ParseStart (["/led" , "/count" , "/coffee" ])
5454 HTTP_PARSER_START = 0
5555 connect (m , start_matcher .input , parser_demux .outs [HTTP_PARSER_START ])
5656
@@ -72,7 +72,7 @@ def elaborate(self, _platform):
7272 m .d .comb += parser_demux .outs [HTTP_PARSER_SINK ].ready .eq (1 )
7373
7474 ## Responders
75- response_mux = m .submodules .response_mux = StreamMux (mux_width = 4 , stream_width = 8 )
75+ response_mux = m .submodules .response_mux = StreamMux (mux_width = 5 , stream_width = 8 )
7676 connect (m , response_mux .out , self .session .outbound .data )
7777 count_body = m .submodules .count_body = CountBody ()
7878
@@ -130,7 +130,25 @@ def elaborate(self, _platform):
130130 count_body .inc_error .eq (1 ),
131131 ]
132132
133- RESPONSE_COUNT = 3
133+ teapot_response = "\r \n " .join (
134+ ["HTTP/1.0 418 I'm a teapot" ,
135+ "Host: Fomu" ,
136+ "Content-Type: text/plain; charset=utf-8" ,
137+ "" ,
138+ "" ,
139+ "short and stout" ]) + "\r \n "
140+ teapot_response = teapot_response .encode ("utf-8" )
141+ teapot_printer = m .submodules .teapot_printer = Printer (teapot_response )
142+ RESPONSE_TEAPOT = 3
143+ connect (m , teapot_printer .output , response_mux .input [RESPONSE_TEAPOT ])
144+ send_teapot = [
145+ response_mux .select .eq (RESPONSE_TEAPOT ),
146+ parser_demux .select .eq (HTTP_PARSER_SINK ),
147+ teapot_printer .en .eq (1 ),
148+ count_body .inc_error .eq (1 ),
149+ ]
150+
151+ RESPONSE_COUNT = 4
134152 connect (m , count_body .output , response_mux .input [RESPONSE_COUNT ])
135153 send_count = [
136154 response_mux .select .eq (RESPONSE_COUNT ),
@@ -185,6 +203,14 @@ def elaborate(self, _platform):
185203 with m .Else ():
186204 m .next = "writing"
187205 m .d .sync += send_405
206+ with m .Elif (start_matcher .path [MATCHED_COFFEE_PATH ]):
207+ with m .If (start_matcher .method [start_matcher .METHOD_GET ]
208+ | start_matcher .method [start_matcher .METHOD_BREW ]):
209+ m .next = "writing"
210+ m .d .sync += send_teapot
211+ with m .Else ():
212+ m .next = "writing"
213+ m .d .sync += send_405
188214 with m .Else ():
189215 m .next = "writing"
190216 m .d .sync += send_404
@@ -218,6 +244,7 @@ def elaborate(self, _platform):
218244 ok_printer .en .eq (0 ),
219245 not_found_printer .en .eq (0 ),
220246 not_allowed_printer .en .eq (0 ),
247+ teapot_printer .en .eq (0 ),
221248 count_body .en .eq (0 ),
222249 self .session .outbound .active .eq (1 ),
223250 count_body .inc_ok .eq (0 ),
@@ -226,7 +253,8 @@ def elaborate(self, _platform):
226253 with m .If ( ((response_mux .select == RESPONSE_OK ) & ok_printer .done )
227254 | ((response_mux .select == RESPONSE_404 ) & not_found_printer .done )
228255 | ((response_mux .select == RESPONSE_405 ) & not_allowed_printer .done )
229- | ((response_mux .select == RESPONSE_COUNT ) & count_body .done )):
256+ | ((response_mux .select == RESPONSE_COUNT ) & count_body .done )
257+ | ((response_mux .select == RESPONSE_TEAPOT ) & teapot_printer .done )):
230258 m .d .sync += self .session .outbound .active .eq (0 )
231259 # Can finish writing before all the input is collected,
232260 # since a bad request migh trigger an early 404. Wait
0 commit comments