Skip to content

Commit

Permalink
Code style and comment fixes, as suggested by @theseion
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrichau committed Dec 24, 2023
1 parent e4f4164 commit cbfd208
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,53 @@
private
parseMultiPartFieldWithoutLengthWithBoundary: aBoundary writeOn: writer

| bufferSize rawBuffer buffer boundaryMarker |
"Can't simply use a larger buffer as we don't want to read past a boundary,
those bytes will belong to the next part"
"Can't simply use a larger buffer as we don't want to read past a boundary, those bytes will belong to the next part"
bufferSize := aBoundary size.
rawBuffer := ByteArray new: bufferSize.
buffer := GRPlatform current ringBufferClass on: rawBuffer.
boundaryMarker := aBoundary first.

stream atEnd ifFalse: [
"#next:into: answers a copy of the buffer if not enough bytes could be read"
"Use rawBuffer here so we can use the primitive to replace the bytes in the
buffer directly"
stream
next: bufferSize
into: rawBuffer ].
"#next:into: answers a copy of the buffer if not enough bytes could be read.
Use rawBuffer here so we can use the primitive to replace the bytes in the buffer directly"
stream next: bufferSize into: rawBuffer ].

[ (buffer
indexOf: boundaryMarker
ifAbsent: [ nil ])
ifNotNil: [ :boundaryCandidateIndex |
(boundaryCandidateIndex = 1 and: [
buffer = aBoundary ])
ifTrue: [
"Found a boundary. We're done"
true ]
ifFalse: [
| candidateIndex |
candidateIndex := boundaryCandidateIndex = 1
ifTrue: [ 2 ]
ifFalse: [ boundaryCandidateIndex ].
"Write all the bytes that we know are not part of a boundary"
1
to: candidateIndex - 1
do: [ :index | writer nextPut: (buffer at: index) ].
"Move the rest of the buffer to the beginning of the buffer"
buffer moveStartTo: candidateIndex.

"Fill the rest of the buffer"
"Use rawBuffer here so we can use the primitive to replace the bytes in the
buffer directly"
rawBuffer := buffer
copyFrom: 1
to: bufferSize.
stream
next: candidateIndex - 1
into: rawBuffer
startingAt: bufferSize - candidateIndex + 2.
buffer initializeWithCollection: rawBuffer.
"If the candidate was really the first token of the boundary
[
(buffer indexOf: boundaryMarker ifAbsent: [ nil ])
ifNotNil: [ :boundaryCandidateIndex |
(boundaryCandidateIndex = 1 and: [ buffer = aBoundary ])
ifTrue: [ "Found a boundary. We're done" true ]
ifFalse: [
| candidateIndex |
candidateIndex := boundaryCandidateIndex = 1
ifTrue: [ 2 ]
ifFalse: [ boundaryCandidateIndex ].
"Write all the bytes that we know are not part of a boundary"
1 to: candidateIndex - 1 do: [ :index |
writer nextPut: (buffer at: index) ].
"Move the rest of the buffer to the beginning of the buffer"
buffer moveStartTo: candidateIndex.
"Fill the rest of the buffer.
Use rawBuffer here so we can use the primitive to replace the bytes in the
buffer directly"
rawBuffer := buffer copyFrom: 1 to: bufferSize.
stream
next: candidateIndex - 1
into: rawBuffer
startingAt: bufferSize - candidateIndex + 2.
buffer initializeWithCollection: rawBuffer.
"If the candidate was really the first token of the boundary
then we now have loaded the full boundary into the buffer.
If not we have to check for the next boundary candidate as
we might have loaded the next boundary partially."
buffer = aBoundary ] ]
ifNil: [
writer nextPutAll: buffer.
stream atEnd
ifFalse: [
"#next:into: answers a copy of the buffer if not enough bytes could be read"
"Use rawBuffer here so we can use the primitive to replace the bytes in the
buffer = aBoundary ] ]
ifNil: [
writer nextPutAll: buffer.
stream atEnd
ifFalse: [ "#next:into: answers a copy of the buffer if not enough bytes could be read""Use rawBuffer here so we can use the primitive to replace the bytes in the
buffer directly"
stream
next: bufferSize
into: rawBuffer.
buffer moveStartTo: 1.
false ]
ifTrue: [ true ] ] ] whileFalse
stream next: bufferSize into: rawBuffer.
buffer moveStartTo: 1.
false ]
ifTrue: [ true ] ] ] whileFalse
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ partsDecodeWith: aDecoderBlock decodeFilesWith: aFileDecoderBlock
part := ZnMimePart new
headers: (ZnHeaders readFrom: stream);
yourself.
"When a value is empty (see comment in #parseMultiPartFieldWithoutLengthWithBoundary:writeOn:)
and it is the last part then parsing headers will consumed the rest of the end boundary and the
stream will be at the end. In this case, the part will not have any headers"
"When a part is empty and it is the last part then parsing headers will have consumed
the rest of the end boundary and the stream will be at the end.
In this case, the part will not have any headers."
(part hasHeaders not or: [
"The final boundary may be read as a header when
the last part was not a file part"
"The final boundary may have been read as a header when the last part was not a file part"
part headers includesKey: endBoundary ]) ifFalse: [
(part fileName notNil or: [
part contentType notNil and: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
initialize-release
initialization
configureDelegate
"Set the main delegate of my server to the default one."

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
initialize-release
initialization
configureServerForBinaryReading
"Seaside wants to do its own text conversions"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
initialize-release
initialization

Check warning on line 1 in repository/Seaside-Zinc-Core.package/ZnZincServerAdaptor.class/instance/server..st

View check run for this annotation

Codecov / codecov/patch

repository/Seaside-Zinc-Core.package/ZnZincServerAdaptor.class/instance/server..st#L1

Added line #L1 was not covered by tests
server: znServer
"I initialize the actual ZnServer instance that I will be using to znServer,
to prevent a fallback to #defaultZnServer"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
initialize-release
initialization

Check warning on line 1 in repository/Seaside-Zinc-Core.package/ZnZincServerAdaptor.class/instance/serverClass..st

View check run for this annotation

Codecov / codecov/patch

repository/Seaside-Zinc-Core.package/ZnZincServerAdaptor.class/instance/serverClass..st#L1

Added line #L1 was not covered by tests
serverClass: znServerClass
"I initialize the actual ZnServer instance that I will be using,
by instanciating znServerClass using my port"
Expand Down

0 comments on commit cbfd208

Please sign in to comment.