storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/genmessage.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package s3select
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/binary"
    25  	"fmt"
    26  	"hash/crc32"
    27  )
    28  
    29  func genRecordsHeader() {
    30  	buf := new(bytes.Buffer)
    31  
    32  	buf.WriteByte(13)
    33  	buf.WriteString(":message-type")
    34  	buf.WriteByte(7)
    35  	buf.Write([]byte{0, 5})
    36  	buf.WriteString("event")
    37  
    38  	buf.WriteByte(13)
    39  	buf.WriteString(":content-type")
    40  	buf.WriteByte(7)
    41  	buf.Write([]byte{0, 24})
    42  	buf.WriteString("application/octet-stream")
    43  
    44  	buf.WriteByte(11)
    45  	buf.WriteString(":event-type")
    46  	buf.WriteByte(7)
    47  	buf.Write([]byte{0, 7})
    48  	buf.WriteString("Records")
    49  
    50  	fmt.Println(buf.Bytes())
    51  }
    52  
    53  // Continuation Message
    54  // ====================
    55  // Header specification
    56  // --------------------
    57  // Continuation messages contain two headers, as follows:
    58  // https://docs.aws.amazon.com/AmazonS3/latest/API/images/s3select-frame-diagram-cont.png
    59  //
    60  // Payload specification
    61  // ---------------------
    62  // Continuation messages have no payload.
    63  func genContinuationMessage() {
    64  	buf := new(bytes.Buffer)
    65  
    66  	buf.WriteByte(13)
    67  	buf.WriteString(":message-type")
    68  	buf.WriteByte(7)
    69  	buf.Write([]byte{0, 5})
    70  	buf.WriteString("event")
    71  
    72  	buf.WriteByte(11)
    73  	buf.WriteString(":event-type")
    74  	buf.WriteByte(7)
    75  	buf.Write([]byte{0, 4})
    76  	buf.WriteString("Cont")
    77  
    78  	header := buf.Bytes()
    79  	headerLength := len(header)
    80  	payloadLength := 0
    81  	totalLength := totalByteLength(headerLength, payloadLength)
    82  
    83  	buf = new(bytes.Buffer)
    84  	binary.Write(buf, binary.BigEndian, uint32(totalLength))
    85  	binary.Write(buf, binary.BigEndian, uint32(headerLength))
    86  	prelude := buf.Bytes()
    87  	binary.Write(buf, binary.BigEndian, crc32.ChecksumIEEE(prelude))
    88  	buf.Write(header)
    89  	message := buf.Bytes()
    90  	binary.Write(buf, binary.BigEndian, crc32.ChecksumIEEE(message))
    91  
    92  	fmt.Println(buf.Bytes())
    93  }
    94  
    95  func genProgressHeader() {
    96  	buf := new(bytes.Buffer)
    97  
    98  	buf.WriteByte(13)
    99  	buf.WriteString(":message-type")
   100  	buf.WriteByte(7)
   101  	buf.Write([]byte{0, 5})
   102  	buf.WriteString("event")
   103  
   104  	buf.WriteByte(13)
   105  	buf.WriteString(":content-type")
   106  	buf.WriteByte(7)
   107  	buf.Write([]byte{0, 8})
   108  	buf.WriteString("text/xml")
   109  
   110  	buf.WriteByte(11)
   111  	buf.WriteString(":event-type")
   112  	buf.WriteByte(7)
   113  	buf.Write([]byte{0, 8})
   114  	buf.WriteString("Progress")
   115  
   116  	fmt.Println(buf.Bytes())
   117  }
   118  
   119  func genStatsHeader() {
   120  	buf := new(bytes.Buffer)
   121  
   122  	buf.WriteByte(13)
   123  	buf.WriteString(":message-type")
   124  	buf.WriteByte(7)
   125  	buf.Write([]byte{0, 5})
   126  	buf.WriteString("event")
   127  
   128  	buf.WriteByte(13)
   129  	buf.WriteString(":content-type")
   130  	buf.WriteByte(7)
   131  	buf.Write([]byte{0, 8})
   132  	buf.WriteString("text/xml")
   133  
   134  	buf.WriteByte(11)
   135  	buf.WriteString(":event-type")
   136  	buf.WriteByte(7)
   137  	buf.Write([]byte{0, 5})
   138  	buf.WriteString("Stats")
   139  
   140  	fmt.Println(buf.Bytes())
   141  }
   142  
   143  // End Message
   144  // ===========
   145  // Header specification
   146  // --------------------
   147  // End messages contain two headers, as follows:
   148  // https://docs.aws.amazon.com/AmazonS3/latest/API/images/s3select-frame-diagram-end.png
   149  //
   150  // Payload specification
   151  // ---------------------
   152  // End messages have no payload.
   153  func genEndMessage() {
   154  	buf := new(bytes.Buffer)
   155  
   156  	buf.WriteByte(13)
   157  	buf.WriteString(":message-type")
   158  	buf.WriteByte(7)
   159  	buf.Write([]byte{0, 5})
   160  	buf.WriteString("event")
   161  
   162  	buf.WriteByte(11)
   163  	buf.WriteString(":event-type")
   164  	buf.WriteByte(7)
   165  	buf.Write([]byte{0, 3})
   166  	buf.WriteString("End")
   167  
   168  	header := buf.Bytes()
   169  	headerLength := len(header)
   170  	payloadLength := 0
   171  	totalLength := totalByteLength(headerLength, payloadLength)
   172  
   173  	buf = new(bytes.Buffer)
   174  	binary.Write(buf, binary.BigEndian, uint32(totalLength))
   175  	binary.Write(buf, binary.BigEndian, uint32(headerLength))
   176  	prelude := buf.Bytes()
   177  	binary.Write(buf, binary.BigEndian, crc32.ChecksumIEEE(prelude))
   178  	buf.Write(header)
   179  	message := buf.Bytes()
   180  	binary.Write(buf, binary.BigEndian, crc32.ChecksumIEEE(message))
   181  
   182  	fmt.Println(buf.Bytes())
   183  }