Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion http_server/count_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def elaborate(self, _platform):
m.submodules.printer = printer = PrinterSeq([
Printer("requests: "), count_req,
Printer(" ok_responses: "), count_ok,
Printer(" error_responses: "), count_error
Printer(" error_responses: "), count_error,
Printer("\r\n")
])

m.d.comb += [
Expand Down
2 changes: 1 addition & 1 deletion http_server/count_body_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_count_body():
dut = CountBody()
expected = "requests: 0003 ok_responses: 0002 error_responses: 0001"
expected = "requests: 0003 ok_responses: 0002 error_responses: 0001\r\n"

sim = Simulator(dut)
sim.add_clock(1e-6)
Expand Down
4 changes: 0 additions & 4 deletions http_server/simple_led_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def elaborate(self, _platform):
"Host: Fomu",
"Content-Type: text/plain; charset=utf-8",
"",
"",
'👍']) + "\r\n"
ok_response = ok_response.encode("utf-8")
ok_printer = m.submodules.ok_printer = Printer(ok_response)
Expand All @@ -99,7 +98,6 @@ def elaborate(self, _platform):
"Host: Fomu",
"Content-Type: text/plain; charset=utf-8",
"",
"",
'👎']) + "\r\n"
not_found_response = not_found_response.encode("utf-8")
not_found_printer = m.submodules.not_found_printer = Printer(not_found_response)
Expand All @@ -117,7 +115,6 @@ def elaborate(self, _platform):
"Host: Fomu",
"Content-Type: text/plain; charset=utf-8",
"",
"",
'🛑']) + "\r\n"
not_allowed_response = not_allowed_response.encode("utf-8")
not_allowed_printer = m.submodules.not_allowed_printer = Printer(not_allowed_response)
Expand All @@ -135,7 +132,6 @@ def elaborate(self, _platform):
"Host: Fomu",
"Content-Type: text/plain; charset=utf-8",
"",
"",
"short and stout"]) + "\r\n"
teapot_response = teapot_response.encode("utf-8")
teapot_printer = m.submodules.teapot_printer = Printer(teapot_response)
Expand Down
21 changes: 3 additions & 18 deletions http_server/simple_led_http_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ def test_ok_handling():
"User-Agent: test-agent\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"\r\n"
"123456\r\n")
expected_output = ("HTTP/1.0 200 OK\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"👍\r\n")

async def driver(ctx):
Expand Down Expand Up @@ -57,13 +55,12 @@ async def driver(ctx):

# Doesn't appear to be a way to _remove_ a testbench;
# I guess .reset() is "just" to allow a different initial state?
with sim.write_vcd(sys.stdout):
sim.run_until(0.001)
#with sim.write_vcd("test.vcd"):
sim.run_until(0.0005)

# Now that the test is done:
collector.assert_eq(expected_output)


def test_404_handling():
dut = SimpleLedHttp()
sim = Simulator(dut)
Expand All @@ -74,13 +71,11 @@ def test_404_handling():
"User-Agent: evil-agent\r\n"
"Content-Type: text/bad\r\n"
"\r\n"
"\r\n"
"123456\r\n")
expected_output = ("HTTP/1.0 404 Not Found\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"👎\r\n")

async def driver(ctx):
Expand Down Expand Up @@ -125,13 +120,11 @@ def test_405_handling():
"User-Agent: evil-agent\r\n"
"Content-Type: text/bad\r\n"
"\r\n"
"\r\n"
"What're your LEDs doing?\r\n")
expected_output = ("HTTP/1.0 405 Method Not Allowed\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"🛑\r\n")

async def driver(ctx):
Expand Down Expand Up @@ -175,42 +168,36 @@ def test_count_handling():
"User-Agent: test-agent\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"\r\n"
"123456\r\n")
error_input = ("BREW /cocoa HTTP/1.0\r\n"
"Host: test\r\n"
"User-Agent: test-agent\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"\r\n"
"With marshmallows, please\r\n")
count_input = ("GET /count HTTP/1.0\r\n"
"Host: test\r\n"
"User-Agent: test-agent\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"\r\n"
"\r\n")

expected_output = ("HTTP/1.0 200 OK\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"👍\r\n"
"HTTP/1.0 404 Not Found\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"👎\r\n"
"HTTP/1.0 200 OK\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"👍\r\n"
"requests: 0003 ok_responses: 0002 error_responses: 0001")
"requests: 0003 ok_responses: 0002 error_responses: 0001\r\n")

async def driver(ctx):

Expand Down Expand Up @@ -263,13 +250,11 @@ def test_coffee_handling():
"User-Agent: evil-agent\r\n"
"Content-Type: text/bad\r\n"
"\r\n"
"\r\n"
"Black, medium roast Ethiopian, pour over\r\n")
expected_output = ("HTTP/1.0 418 I'm a teapot\r\n"
"Host: Fomu\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"\r\n"
"short and stout\r\n")

async def driver(ctx):
Expand Down
28 changes: 13 additions & 15 deletions http_server/string_contains_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,26 @@ def elaborate(self, _platform):

shift_reg = [Signal(8) for _ in range(len(self._message))]

latched_accept = Signal(1)

matched = Signal(len(self._message))
m.d.comb += matched[0].eq(c == self._message[len(self._message)-1])
for i in range(1,len(self._message)):
m.d.comb += matched[i].eq(shift_reg[i-1] == self._message[len(self._message)-i-1])

with m.If(matched.all()):
m.d.sync += latched_accept.eq(1)
m.d.comb += self.accepted.eq(matched.all() | latched_accept)


with m.If(self.reset):
m.d.sync += self.accepted.eq(Const(0))
m.d.sync += latched_accept.eq(0)
for i in range(len(self._message)):
m.d.sync += shift_reg[i].eq(0)
with m.Elif(self.input.valid):
for i in range(len(self._message)-2,-1,-1):
m.d.sync += shift_reg[i+1].eq(shift_reg[i])
m.d.sync += shift_reg[0].eq(c)

matched = [Signal(1) for _ in range(len(self._message))]
for i in range(len(self._message)):
m.d.comb += matched[i].eq(shift_reg[i] == self._message[len(self._message)-i-1])

# Note: Doing a linear reduction here. Could be a problem for very long
# matches. If it ends up being an issue, switch to a tree.
matched_chain = [Signal(1) for _ in range(len(self._message)+1)]
m.d.comb += matched_chain[0].eq(Const(1))
for i in range(len(self._message)):
m.d.comb += matched_chain[i+1].eq(matched_chain[i] & matched[i])

# Latch acceptance
with m.If(matched_chain[len(self._message)]):
m.d.sync += self.accepted.eq(1)

return m