github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/textproto/textproto.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // パッケージtextprotoは、HTTP、NNTP、およびSMTPのスタイルでテキストベースのリクエスト/レスポンスプロトコルの汎用サポートを実装します。 6 // 7 // このパッケージでは以下を提供します: 8 // 9 // [Error] は、サーバーからの数値エラーレスポンスを表します。 10 // 11 // [Pipeline] は、クライアントでパイプライン化されたリクエストとレスポンスを管理するためのものです。 12 // 13 // [Reader] は、数値応答コードライン、キー: 値のヘッダ、先行スペースで折り返された行、独自の行にドットで終わる全文テキストブロックを読み取るためのものです。 14 // 15 // [Writer] は、ドットエンコードされたテキストブロックを書き込むためのものです。 16 // 17 // [Conn] は、単一のネットワーク接続で使用するための、[Reader]、[Writer]、および [Pipeline] の便利なパッケージングです。 18 package textproto 19 20 import ( 21 "github.com/shogo82148/std/io" 22 ) 23 24 // Errorは、サーバーからの数値エラーレスポンスを表します。 25 type Error struct { 26 Code int 27 Msg string 28 } 29 30 func (e *Error) Error() string 31 32 // ProtocolErrorは、無効なレスポンスや切断された接続など、プロトコル違反を示すものです。 33 type ProtocolError string 34 35 func (p ProtocolError) Error() string 36 37 // Connはテキストネットワークプロトコルの接続を表します。 38 // それは、I/Oを管理するための [Reader] と [Writer]、および接続上で並行リクエストをシーケンスするための [Pipeline] で構成されています。 39 // これらの埋め込まれた型は、それらの型のドキュメントで詳細なメソッドを持っています。 40 type Conn struct { 41 Reader 42 Writer 43 Pipeline 44 conn io.ReadWriteCloser 45 } 46 47 // NewConnはI/Oにconnを使用して新しい [Conn] を返します。 48 func NewConn(conn io.ReadWriteCloser) *Conn 49 50 // Close は接続を閉じます。 51 func (c *Conn) Close() error 52 53 // Dialは、[net.Dial] を使って指定されたネットワークの指定されたアドレスに接続し、接続のための新しい [Conn] を返します。 54 func Dial(network, addr string) (*Conn, error) 55 56 // Cmdはパイプラインの順番を待ってからコマンドを送る便利なメソッドです。コマンドのテキストは、formatとargsを使用してフォーマットし、\r\nを追加した結果です。CmdはコマンドのIDを返し、StartResponseとEndResponseで使用します。 57 // 例えば、クライアントはHELPコマンドを実行し、ドットボディを返すかもしれません: 58 // id, err := c.Cmd("HELP") 59 // 60 // if err != nil { 61 // return nil, err 62 // } 63 // 64 // c.StartResponse(id) 65 // defer c.EndResponse(id) 66 // 67 // if _, _, err = c.ReadCodeLine(110); err != nil { 68 // return nil, err 69 // } 70 // 71 // text, err := c.ReadDotBytes() 72 // 73 // if err != nil { 74 // return nil, err 75 // } 76 // 77 // return c.ReadCodeLine(250) 78 func (c *Conn) Cmd(format string, args ...any) (id uint, err error) 79 80 // TrimStringは、先頭と末尾のASCIIスペースを除いたsを返します。 81 func TrimString(s string) string 82 83 // TrimBytesは、先頭と末尾のASCIIスペースを除いたbを返します。 84 func TrimBytes(b []byte) []byte