-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* hw1 first try * hw1 first try * hw1 first try * hw2 first try * hw2 first try * hw2 string templates removed * hw2 codeclimate * hw2 codeclimate * hw2 report * hw3 codeDrop * hw3 codeDrop * hw3 some fixes * hw3 test fixes and changes using ref * hw3 codeclimate fixes * hw3 codeclimate fixes * hw3 report * hw3 report * hw4 codeDrop * hw4 codeDrop * hw4 works * hw4 codeclimate * hw4 report * hw4 report * codeDrop * codeDrop * codeDrop * codeDrop * codeDrop * two tests not working * green tests * started refactoring * refactoring * code climate * code climate * code climate * code climate * code climate * code climate * code climate * code climate * code climate * hw5 report * hw5 fixes, not all yet * hw5 fixes, not all yet * hw5 fixes, not all yet * hw5 fixes * hw5 climate * hw5 climate * hw5 climate * hw6 wip * hw6 wip * hw6 * hw6 * hw6 report'ik * hw6 report'ik --------- Co-authored-by: Vadim Tsesko <[email protected]> Co-authored-by: Anton Lamtev <[email protected]>
- Loading branch information
1 parent
8664da6
commit bdfd74f
Showing
67 changed files
with
47,587 additions
and
1,401 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/vk/itmo/test/osokindm/ChunkedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ru.vk.itmo.test.osokindm; | ||
|
||
import one.nio.http.Response; | ||
import ru.vk.itmo.test.osokindm.dao.Entry; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.util.Iterator; | ||
|
||
public class ChunkedResponse extends Response { | ||
|
||
private static final String CHUNKED_HEADER = "Transfer-Encoding: chunked"; | ||
private static final String CONTENT_TYPE_HEADER = "Content-Type: result-range"; | ||
private final Iterator<Entry<MemorySegment>> rangeResult; | ||
|
||
public ChunkedResponse(String resultCode, Iterator<Entry<MemorySegment>> rangeResult) { | ||
super(resultCode); | ||
super.addHeader(CHUNKED_HEADER); | ||
super.addHeader(CONTENT_TYPE_HEADER); | ||
this.rangeResult = rangeResult; | ||
} | ||
|
||
public Iterator<Entry<MemorySegment>> getResultIterator() { | ||
return rangeResult; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/ru/vk/itmo/test/osokindm/CustomHttpSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ru.vk.itmo.test.osokindm; | ||
|
||
import one.nio.http.HttpServer; | ||
import one.nio.http.HttpSession; | ||
import one.nio.http.Response; | ||
import one.nio.net.Socket; | ||
import ru.vk.itmo.test.osokindm.dao.Entry; | ||
|
||
import java.io.IOException; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Iterator; | ||
|
||
public class CustomHttpSession extends HttpSession { | ||
|
||
private static final byte[] CHUNK_SEPARATOR = "\r\n".getBytes(StandardCharsets.UTF_8); | ||
private static final byte[] DELIMITER = "\n".getBytes(StandardCharsets.UTF_8); | ||
private static final byte[] ZERO = "0".getBytes(StandardCharsets.UTF_8); | ||
|
||
public CustomHttpSession(Socket socket, HttpServer server) { | ||
super(socket, server); | ||
} | ||
|
||
@Override | ||
protected void writeResponse(Response response, boolean includeBody) throws IOException { | ||
if (response instanceof ChunkedResponse) { | ||
super.writeResponse(response, false); | ||
Iterator<Entry<MemorySegment>> iterator = ((ChunkedResponse) response).getResultIterator(); | ||
while (iterator.hasNext()) { | ||
writeChunk(iterator.next()); | ||
} | ||
|
||
super.write(ZERO, 0, ZERO.length); | ||
super.write(CHUNK_SEPARATOR, 0, CHUNK_SEPARATOR.length); | ||
super.write(CHUNK_SEPARATOR, 0, CHUNK_SEPARATOR.length); | ||
super.close(); | ||
} else { | ||
super.writeResponse(response, includeBody); | ||
} | ||
} | ||
|
||
private void writeChunk(Entry<MemorySegment> entry) throws IOException { | ||
byte[] keyBytes = entry.key().toArray(ValueLayout.JAVA_BYTE); | ||
byte[] valueBytes = entry.value().toArray(ValueLayout.JAVA_BYTE); | ||
int entryLength = keyBytes.length + valueBytes.length + DELIMITER.length; | ||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + entryLength + CHUNK_SEPARATOR.length * 2); | ||
buffer.put(Integer.toHexString(entryLength).getBytes(StandardCharsets.UTF_8)); | ||
buffer.put(CHUNK_SEPARATOR); | ||
buffer.put(entry.key().toArray(ValueLayout.JAVA_BYTE)); | ||
buffer.put(DELIMITER); | ||
buffer.put(entry.value().toArray(ValueLayout.JAVA_BYTE)); | ||
buffer.put(CHUNK_SEPARATOR); | ||
|
||
super.write(buffer.array(), 0, buffer.position()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.