~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Transport/TransportHttp.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
                        {
131
131
                                return new NGit.Transport.TransportHttp(local, uri);
132
132
                        }
 
133
 
 
134
                        /// <exception cref="System.NotSupportedException"></exception>
 
135
                        public override NGit.Transport.Transport Open(URIish uri)
 
136
                        {
 
137
                                return new NGit.Transport.TransportHttp(uri);
 
138
                }
133
139
                }
134
140
 
135
141
                internal static readonly TransportProtocol PROTO_HTTP = new _TransportProtocol_137
136
142
                        ();
137
143
 
138
 
                private sealed class _TransportProtocol_172 : TransportProtocol
 
144
                private sealed class _TransportProtocol_176 : TransportProtocol
139
145
                {
140
 
                        public _TransportProtocol_172()
 
146
                        public _TransportProtocol_176()
141
147
                        {
142
148
                        }
143
149
 
177
183
                        }
178
184
                }
179
185
 
180
 
                internal static readonly TransportProtocol PROTO_FTP = new _TransportProtocol_172
 
186
                internal static readonly TransportProtocol PROTO_FTP = new _TransportProtocol_176
181
187
                        ();
182
188
 
183
189
                private static string ComputeUserAgent()
196
202
                        return "JGit/" + version;
197
203
                }
198
204
 
199
 
                private sealed class _SectionParser_212 : Config.SectionParser<TransportHttp.HttpConfig
 
205
                private sealed class _SectionParser_216 : Config.SectionParser<TransportHttp.HttpConfig
200
206
                        >
201
207
                {
202
 
                        public _SectionParser_212()
 
208
                        public _SectionParser_216()
203
209
                        {
204
210
                        }
205
211
 
211
217
                }
212
218
 
213
219
                private static readonly Config.SectionParser<TransportHttp.HttpConfig> HTTP_KEY = 
214
 
                        new _SectionParser_212();
 
220
                        new _SectionParser_216();
215
221
 
216
222
                private class HttpConfig
217
223
                {
225
231
                                //$NON-NLS-1$  //$NON-NLS-2$
226
232
                                sslVerify = rc.GetBoolean("http", "sslVerify", true);
227
233
                        }
 
234
 
 
235
                        public HttpConfig() : this(new Config())
 
236
                        {
 
237
                }
228
238
                }
229
239
 
230
240
                private readonly Uri baseUrl;
264
274
                        proxySelector = ProxySelector.GetDefault();
265
275
                }
266
276
 
 
277
                /// <summary>Create a minimal HTTP transport with default configuration values.</summary>
 
278
                /// <remarks>Create a minimal HTTP transport with default configuration values.</remarks>
 
279
                /// <param name="uri"></param>
 
280
                /// <exception cref="System.NotSupportedException">System.NotSupportedException</exception>
 
281
                protected internal TransportHttp(URIish uri) : base(uri)
 
282
                {
 
283
                        try
 
284
                        {
 
285
                                string uriString = uri.ToString();
 
286
                                if (!uriString.EndsWith("/"))
 
287
                                {
 
288
                                        //$NON-NLS-1$
 
289
                                        uriString += "/";
 
290
                                }
 
291
                                //$NON-NLS-1$
 
292
                                baseUrl = new Uri(uriString);
 
293
                                objectsUrl = new Uri(baseUrl, "objects/");
 
294
                        }
 
295
                        catch (UriFormatException e)
 
296
                        {
 
297
                                //$NON-NLS-1$
 
298
                                throw new NGit.Errors.NotSupportedException(MessageFormat.Format(JGitText.Get().invalidURL, uri
 
299
                                        ), e);
 
300
                        }
 
301
                        http = new TransportHttp.HttpConfig();
 
302
                        proxySelector = ProxySelector.GetDefault();
 
303
                }
 
304
 
267
305
                /// <summary>Toggle whether or not smart HTTP transport should be used.</summary>
268
306
                /// <remarks>
269
307
                /// Toggle whether or not smart HTTP transport should be used.
392
430
                                        default:
393
431
                                        {
394
432
                                                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().cannotReadHEAD
395
 
                                                        , status, conn.GetResponseMessage()));
 
433
                                                        , Sharpen.Extensions.ValueOf(status), conn.GetResponseMessage()));
396
434
                                        }
397
435
                                }
398
436
                        }
590
628
                        conn.SetRequestProperty(HttpSupport.HDR_PRAGMA, "no-cache");
591
629
                        //$NON-NLS-1$
592
630
                        conn.SetRequestProperty(HttpSupport.HDR_USER_AGENT, userAgent);
593
 
                        conn.SetConnectTimeout(GetTimeout() * 1000);
594
 
                        conn.SetReadTimeout(GetTimeout() * 1000);
 
631
                        int timeOut = GetTimeout();
 
632
                        if (timeOut != -1)
 
633
                        {
 
634
                                int effTimeOut = timeOut * 1000;
 
635
                                conn.SetConnectTimeout(effTimeOut);
 
636
                                conn.SetReadTimeout(effTimeOut);
 
637
                        }
595
638
                        authMethod.ConfigureRequest(conn);
596
639
                        return conn;
597
640
                }
642
685
                        string expType = "application/x-" + service + "-advertisement";
643
686
                        //$NON-NLS-1$ //$NON-NLS-2$
644
687
                        string actType = c.GetContentType();
645
 
                        return expType.Equals(actType);
 
688
                        return expType.Contains(actType);
646
689
                }
647
690
 
648
691
                /// <exception cref="System.IO.IOException"></exception>
864
907
 
865
908
                internal class SmartHttpFetchConnection : BasePackFetchConnection
866
909
                {
867
 
                        private TransportHttp.Service svc;
 
910
                        private TransportHttp.MultiRequestService svc;
868
911
 
869
912
                        /// <exception cref="NGit.Errors.TransportException"></exception>
870
913
                        internal SmartHttpFetchConnection(TransportHttp _enclosing, InputStream advertisement
883
926
                        {
884
927
                                try
885
928
                                {
886
 
                                        this.svc = new TransportHttp.Service(_enclosing, TransportHttp.SVC_UPLOAD_PACK);
887
 
                                        this.Init(this.svc.@in, this.svc.@out);
 
929
                                        this.svc = new TransportHttp.MultiRequestService(_enclosing, TransportHttp.SVC_UPLOAD_PACK
 
930
                                                );
 
931
                                        this.Init(this.svc.GetInputStream(), this.svc.GetOutputStream());
888
932
                                base.DoFetch(monitor, want, have);
889
933
                        }
890
934
                                finally
918
962
                        protected internal override void DoPush(ProgressMonitor monitor, IDictionary<string
919
963
                                , RemoteRefUpdate> refUpdates)
920
964
                        {
921
 
                                TransportHttp.Service svc = new TransportHttp.Service(_enclosing, TransportHttp.SVC_RECEIVE_PACK
922
 
                                        );
923
 
                                this.Init(svc.@in, svc.@out);
 
965
                                TransportHttp.Service svc = new TransportHttp.MultiRequestService(_enclosing, TransportHttp
 
966
                                        .SVC_RECEIVE_PACK);
 
967
                                this.Init(svc.GetInputStream(), svc.GetOutputStream());
924
968
                                base.DoPush(monitor, refUpdates);
925
969
                        }
926
970
 
927
971
                        private readonly TransportHttp _enclosing;
928
972
                }
929
973
 
930
 
                /// <summary>State required to speak multiple HTTP requests with the remote.</summary>
931
 
                /// <remarks>
932
 
                /// State required to speak multiple HTTP requests with the remote.
933
 
                /// <p>
934
 
                /// A service wrapper provides a normal looking InputStream and OutputStream
935
 
                /// pair which are connected via HTTP to the named remote service. Writing to
936
 
                /// the OutputStream is buffered until either the buffer overflows, or
937
 
                /// reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
938
 
                /// chunked transfer encoding is used to stream the request data to the
939
 
                /// remote service. If the entire request fits in the memory buffer, the
940
 
                /// older HTTP/1.0 standard and a fixed content length is used instead.
941
 
                /// <p>
942
 
                /// It is an error to attempt to read without there being outstanding data
943
 
                /// ready for transmission on the OutputStream.
944
 
                /// <p>
945
 
                /// No state is preserved between write-read request pairs. The caller is
946
 
                /// responsible for replaying state vector information as part of the request
947
 
                /// data written to the OutputStream. Any session HTTP cookies may or may not
948
 
                /// be preserved between requests, it is left up to the JVM's implementation
949
 
                /// of the HTTP client.
950
 
                /// </remarks>
951
 
                internal class Service
 
974
                /// <summary>Basic service for sending and receiving HTTP requests.</summary>
 
975
                /// <remarks>Basic service for sending and receiving HTTP requests.</remarks>
 
976
                internal abstract class Service
952
977
                {
953
 
                        private readonly string serviceName;
954
 
 
955
 
                        private readonly string requestType;
956
 
 
957
 
                        private readonly string responseType;
958
 
 
959
 
                        private readonly TransportHttp.Service.HttpExecuteStream execute;
960
 
 
961
 
                        internal bool finalRequest;
 
978
                        protected internal readonly string serviceName;
 
979
 
 
980
                        protected internal readonly string requestType;
 
981
 
 
982
                        protected internal readonly string responseType;
 
983
 
 
984
                        protected internal HttpURLConnection conn;
 
985
 
 
986
                        protected internal TransportHttp.Service.HttpOutputStream @out;
 
987
 
 
988
                        protected internal readonly TransportHttp.Service.HttpExecuteStream execute;
962
989
 
963
990
                        internal readonly UnionInputStream @in;
964
991
 
965
 
                        internal readonly TransportHttp.Service.HttpOutputStream @out;
966
 
 
967
 
                        internal HttpURLConnection conn;
968
 
 
969
992
                        internal Service(TransportHttp _enclosing, string serviceName)
970
993
                        {
971
994
                                this._enclosing = _enclosing;
974
997
                                //$NON-NLS-1$ //$NON-NLS-2$
975
998
                                this.responseType = "application/x-" + serviceName + "-result";
976
999
                                //$NON-NLS-1$ //$NON-NLS-2$
 
1000
                                this.@out = new TransportHttp.Service.HttpOutputStream(this);
977
1001
                                this.execute = new TransportHttp.Service.HttpExecuteStream(this);
978
1002
                                this.@in = new UnionInputStream(this.execute);
979
 
                                this.@out = new TransportHttp.Service.HttpOutputStream(this);
980
1003
                        }
981
1004
 
982
1005
                        /// <exception cref="System.IO.IOException"></exception>
991
1014
                        }
992
1015
 
993
1016
                        /// <exception cref="System.IO.IOException"></exception>
994
 
                        internal virtual void Execute()
 
1017
                        internal virtual void SendRequest()
995
1018
                        {
996
 
                                this.@out.Close();
997
 
                                if (this.conn == null)
998
 
                                {
999
 
                                        if (this.@out.Length() == 0)
1000
 
                                        {
1001
 
                                                // Request output hasn't started yet, but more data is being
1002
 
                                                // requested. If there is no request data buffered and the
1003
 
                                                // final request was already sent, do nothing to ensure the
1004
 
                                                // caller is shown EOF on the InputStream; otherwise an
1005
 
                                                // programming error has occurred within this module.
1006
 
                                                if (this.finalRequest)
1007
 
                                                {
1008
 
                                                        return;
1009
 
                                                }
1010
 
                                                throw new TransportException(this._enclosing.uri, JGitText.Get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported
1011
 
                                                        );
1012
 
                                        }
1013
1019
                                        // Try to compress the content, but only if that is smaller.
1014
1020
                                        TemporaryBuffer buf = new TemporaryBuffer.Heap(this._enclosing.http.postBuffer);
1015
1021
                                        try
1045
1051
                                                httpOut.Close();
1046
1052
                                        }
1047
1053
                                }
1048
 
                                this.@out.Reset();
 
1054
 
 
1055
                        /// <exception cref="System.IO.IOException"></exception>
 
1056
                        internal virtual void OpenResponse()
 
1057
                        {
1049
1058
                                int status = HttpSupport.Response(this.conn);
1050
1059
                                if (status != HttpURLConnection.HTTP_OK)
1051
1060
                                {
1059
1068
                                        this.conn.GetInputStream().Close();
1060
1069
                                        throw this._enclosing.WrongContentType(this.responseType, contentType);
1061
1070
                                }
1062
 
                                this.@in.Add(this._enclosing.OpenInputStream(this.conn));
1063
 
                                if (!this.finalRequest)
1064
 
                                {
1065
 
                                this.@in.Add(this.execute);
1066
 
                                }
1067
 
                                this.conn = null;
 
1071
                                }
 
1072
 
 
1073
                        internal virtual TransportHttp.Service.HttpOutputStream GetOutputStream()
 
1074
                        {
 
1075
                                return this.@out;
 
1076
                                }
 
1077
 
 
1078
                        internal virtual InputStream GetInputStream()
 
1079
                                {
 
1080
                                return this.@in;
 
1081
                                }
 
1082
 
 
1083
                        /// <exception cref="System.IO.IOException"></exception>
 
1084
                        internal abstract void Execute();
 
1085
 
 
1086
                        internal class HttpExecuteStream : InputStream
 
1087
                        {
 
1088
                                /// <exception cref="System.IO.IOException"></exception>
 
1089
                                public override int Read()
 
1090
                                {
 
1091
                                        this._enclosing.Execute();
 
1092
                                        return -1;
 
1093
                                }
 
1094
 
 
1095
                                /// <exception cref="System.IO.IOException"></exception>
 
1096
                                public override int Read(byte[] b, int off, int len)
 
1097
                                {
 
1098
                                        this._enclosing.Execute();
 
1099
                                        return -1;
 
1100
                                }
 
1101
 
 
1102
                                /// <exception cref="System.IO.IOException"></exception>
 
1103
                                public override long Skip(long n)
 
1104
                                {
 
1105
                                        this._enclosing.Execute();
 
1106
                                        return 0;
 
1107
                                }
 
1108
 
 
1109
                                internal HttpExecuteStream(Service _enclosing)
 
1110
                                {
 
1111
                                        this._enclosing = _enclosing;
 
1112
                                }
 
1113
 
 
1114
                                private readonly Service _enclosing;
1068
1115
                        }
1069
1116
 
1070
1117
                        internal class HttpOutputStream : TemporaryBuffer
1086
1133
                                private readonly Service _enclosing;
1087
1134
                        }
1088
1135
 
1089
 
                        internal class HttpExecuteStream : InputStream
1090
 
                        {
1091
 
                                /// <exception cref="System.IO.IOException"></exception>
1092
 
                                public override int Read()
1093
 
                                {
1094
 
                                        this._enclosing.Execute();
1095
 
                                        return -1;
1096
 
                                }
1097
 
 
1098
 
                                /// <exception cref="System.IO.IOException"></exception>
1099
 
                                public override int Read(byte[] b, int off, int len)
1100
 
                                {
1101
 
                                        this._enclosing.Execute();
1102
 
                                        return -1;
1103
 
                                }
1104
 
 
1105
 
                                /// <exception cref="System.IO.IOException"></exception>
1106
 
                                public override long Skip(long n)
1107
 
                                {
1108
 
                                        this._enclosing.Execute();
1109
 
                                        return 0;
1110
 
                                }
1111
 
 
1112
 
                                internal HttpExecuteStream(Service _enclosing)
1113
 
                                {
1114
 
                                        this._enclosing = _enclosing;
1115
 
                                }
1116
 
 
1117
 
                                private readonly Service _enclosing;
 
1136
                        private readonly TransportHttp _enclosing;
 
1137
                }
 
1138
 
 
1139
                /// <summary>State required to speak multiple HTTP requests with the remote.</summary>
 
1140
                /// <remarks>
 
1141
                /// State required to speak multiple HTTP requests with the remote.
 
1142
                /// <p>
 
1143
                /// A service wrapper provides a normal looking InputStream and OutputStream
 
1144
                /// pair which are connected via HTTP to the named remote service. Writing to
 
1145
                /// the OutputStream is buffered until either the buffer overflows, or
 
1146
                /// reading from the InputStream occurs. If overflow occurs HTTP/1.1 and its
 
1147
                /// chunked transfer encoding is used to stream the request data to the
 
1148
                /// remote service. If the entire request fits in the memory buffer, the
 
1149
                /// older HTTP/1.0 standard and a fixed content length is used instead.
 
1150
                /// <p>
 
1151
                /// It is an error to attempt to read without there being outstanding data
 
1152
                /// ready for transmission on the OutputStream.
 
1153
                /// <p>
 
1154
                /// No state is preserved between write-read request pairs. The caller is
 
1155
                /// responsible for replaying state vector information as part of the request
 
1156
                /// data written to the OutputStream. Any session HTTP cookies may or may not
 
1157
                /// be preserved between requests, it is left up to the JVM's implementation
 
1158
                /// of the HTTP client.
 
1159
                /// </remarks>
 
1160
                internal class MultiRequestService : TransportHttp.Service
 
1161
                {
 
1162
                        internal bool finalRequest;
 
1163
 
 
1164
                        internal MultiRequestService(TransportHttp _enclosing, string serviceName) : base
 
1165
                                (_enclosing, serviceName)
 
1166
                        {
 
1167
                                this._enclosing = _enclosing;
 
1168
                        }
 
1169
 
 
1170
                        /// <summary>Keep opening send-receive pairs to the given URI.</summary>
 
1171
                        /// <remarks>Keep opening send-receive pairs to the given URI.</remarks>
 
1172
                        /// <exception cref="System.IO.IOException"></exception>
 
1173
                        internal override void Execute()
 
1174
                        {
 
1175
                                this.@out.Close();
 
1176
                                if (this.conn == null)
 
1177
                                {
 
1178
                                        if (this.@out.Length() == 0)
 
1179
                                        {
 
1180
                                                // Request output hasn't started yet, but more data is being
 
1181
                                                // requested. If there is no request data buffered and the
 
1182
                                                // final request was already sent, do nothing to ensure the
 
1183
                                                // caller is shown EOF on the InputStream; otherwise an
 
1184
                                                // programming error has occurred within this module.
 
1185
                                                if (this.finalRequest)
 
1186
                                                {
 
1187
                                                        return;
 
1188
                                                }
 
1189
                                                throw new TransportException(this._enclosing.uri, JGitText.Get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported
 
1190
                                                        );
 
1191
                                        }
 
1192
                                        this.SendRequest();
 
1193
                                }
 
1194
                                this.@out.Reset();
 
1195
                                this.OpenResponse();
 
1196
                                this.@in.Add(this._enclosing.OpenInputStream(this.conn));
 
1197
                                if (!this.finalRequest)
 
1198
                                {
 
1199
                                        this.@in.Add(this.execute);
 
1200
                                }
 
1201
                                this.conn = null;
 
1202
                        }
 
1203
 
 
1204
                        private readonly TransportHttp _enclosing;
 
1205
                }
 
1206
 
 
1207
                /// <summary>Service for maintaining a single long-poll connection.</summary>
 
1208
                /// <remarks>Service for maintaining a single long-poll connection.</remarks>
 
1209
                internal class LongPollService : TransportHttp.Service
 
1210
                {
 
1211
                        /// <param name="serviceName"></param>
 
1212
                        internal LongPollService(TransportHttp _enclosing, string serviceName) : base(_enclosing, serviceName
 
1213
                                )
 
1214
                        {
 
1215
                                this._enclosing = _enclosing;
 
1216
                        }
 
1217
 
 
1218
                        /// <summary>Only open one send-receive request.</summary>
 
1219
                        /// <remarks>Only open one send-receive request.</remarks>
 
1220
                        /// <exception cref="System.IO.IOException"></exception>
 
1221
                        internal override void Execute()
 
1222
                        {
 
1223
                                this.@out.Close();
 
1224
                                if (this.conn == null)
 
1225
                                {
 
1226
                                        this.SendRequest();
 
1227
                                }
 
1228
                                this.OpenResponse();
 
1229
                                this.@in.Add(this._enclosing.OpenInputStream(this.conn));
1118
1230
                        }
1119
1231
 
1120
1232
                        private readonly TransportHttp _enclosing;