Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
JSON-java/HTTP.java at master · SooLFaa/JSON-java · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
SooLFaa
/
JSON-java
Public
forked from
stleary/JSON-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
JSON-java
/
HTTP.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
163 lines (144 loc) · 5.79 KB
master
Breadcrumbs
JSON-java
/
HTTP.java
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
163 lines (144 loc) · 5.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package
org
.
json
;
/*
Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import
java
.
util
.
Iterator
;
/**
* Convert an HTTP header to a JSONObject and back.
* @author JSON.org
* @version 2014-05-03
*/
public
class
HTTP
{
/** Carriage return/line feed. */
public
static
final
String
CRLF
=
"
\r
\n
"
;
/**
* Convert an HTTP header string into a JSONObject. It can be a request
* header or a response header. A request header will contain
* <pre>{
* Method: "POST" (for example),
* "Request-URI": "/" (for example),
* "HTTP-Version": "HTTP/1.1" (for example)
* }</pre>
* A response header will contain
* <pre>{
* "HTTP-Version": "HTTP/1.1" (for example),
* "Status-Code": "200" (for example),
* "Reason-Phrase": "OK" (for example)
* }</pre>
* In addition, the other parameters in the header will be captured, using
* the HTTP field names as JSON names, so that <pre>
* Date: Sun, 26 May 2002 18:06:04 GMT
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
* Cache-Control: no-cache</pre>
* become
* <pre>{...
* Date: "Sun, 26 May 2002 18:06:04 GMT",
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
* "Cache-Control": "no-cache",
* ...}</pre>
* It does no further checking or conversion. It does not parse dates.
* It does not do '%' transforms on URLs.
* @param string An HTTP header string.
* @return A JSONObject containing the elements and attributes
* of the XML string.
* @throws JSONException
*/
public
static
JSONObject
toJSONObject
(
String
string
)
throws
JSONException
{
JSONObject
jo
=
new
JSONObject
();
HTTPTokener
x
=
new
HTTPTokener
(
string
);
String
token
;
token
=
x
.
nextToken
();
if
(
token
.
toUpperCase
().
startsWith
(
"HTTP"
)) {
// Response
jo
.
put
(
"HTTP-Version"
,
token
);
jo
.
put
(
"Status-Code"
,
x
.
nextToken
());
jo
.
put
(
"Reason-Phrase"
,
x
.
nextTo
(
'\0'
));
x
.
next
();
}
else
{
// Request
jo
.
put
(
"Method"
,
token
);
jo
.
put
(
"Request-URI"
,
x
.
nextToken
());
jo
.
put
(
"HTTP-Version"
,
x
.
nextToken
());
}
// Fields
while
(
x
.
more
()) {
String
name
=
x
.
nextTo
(
':'
);
x
.
next
(
':'
);
jo
.
put
(
name
,
x
.
nextTo
(
'\0'
));
x
.
next
();
}
return
jo
;
}
/**
* Convert a JSONObject into an HTTP header. A request header must contain
* <pre>{
* Method: "POST" (for example),
* "Request-URI": "/" (for example),
* "HTTP-Version": "HTTP/1.1" (for example)
* }</pre>
* A response header must contain
* <pre>{
* "HTTP-Version": "HTTP/1.1" (for example),
* "Status-Code": "200" (for example),
* "Reason-Phrase": "OK" (for example)
* }</pre>
* Any other members of the JSONObject will be output as HTTP fields.
* The result will end with two CRLF pairs.
* @param jo A JSONObject
* @return An HTTP header string.
* @throws JSONException if the object does not contain enough
* information.
*/
public
static
String
toString
(
JSONObject
jo
)
throws
JSONException
{
Iterator
<
String
>
keys
=
jo
.
keys
();
String
string
;
StringBuilder
sb
=
new
StringBuilder
();
if
(
jo
.
has
(
"Status-Code"
) &&
jo
.
has
(
"Reason-Phrase"
)) {
sb
.
append
(
jo
.
getString
(
"HTTP-Version"
));
sb
.
append
(
' '
);
sb
.
append
(
jo
.
getString
(
"Status-Code"
));
sb
.
append
(
' '
);
sb
.
append
(
jo
.
getString
(
"Reason-Phrase"
));
}
else
if
(
jo
.
has
(
"Method"
) &&
jo
.
has
(
"Request-URI"
)) {
sb
.
append
(
jo
.
getString
(
"Method"
));
sb
.
append
(
' '
);
sb
.
append
(
'"'
);
sb
.
append
(
jo
.
getString
(
"Request-URI"
));
sb
.
append
(
'"'
);
sb
.
append
(
' '
);
sb
.
append
(
jo
.
getString
(
"HTTP-Version"
));
}
else
{
throw
new
JSONException
(
"Not enough material for an HTTP header."
);
}
sb
.
append
(
CRLF
);
while
(
keys
.
hasNext
()) {
string
=
keys
.
next
();
if
(!
"HTTP-Version"
.
equals
(
string
) && !
"Status-Code"
.
equals
(
string
) &&
!
"Reason-Phrase"
.
equals
(
string
) && !
"Method"
.
equals
(
string
) &&
!
"Request-URI"
.
equals
(
string
) && !
jo
.
isNull
(
string
)) {
sb
.
append
(
string
);
sb
.
append
(
": "
);
sb
.
append
(
jo
.
getString
(
string
));
sb
.
append
(
CRLF
);
}
}
sb
.
append
(
CRLF
);
return
sb
.
toString
();
}
}
You can’t perform that action at this time.