winlin

HTTP API support JSONP by specifies the query string callback=xxx.

@@ -342,6 +342,7 @@ Remark: @@ -342,6 +342,7 @@ Remark:
342 342
343 ## History 343 ## History
344 344
  345 +* v2.0, 2015-08-22, HTTP API support JSONP by specifies the query string callback=xxx.
345 * v2.0, 2015-08-20, fix [#380](https://github.com/simple-rtmp-server/srs/issues/380), srs-librtmp send sequence header when sps or pps changed. 346 * v2.0, 2015-08-20, fix [#380](https://github.com/simple-rtmp-server/srs/issues/380), srs-librtmp send sequence header when sps or pps changed.
346 * v2.0, 2015-08-18, close [#454](https://github.com/simple-rtmp-server/srs/issues/454), support obs restart publish. 2.0.184 347 * v2.0, 2015-08-18, close [#454](https://github.com/simple-rtmp-server/srs/issues/454), support obs restart publish. 2.0.184
347 * v2.0, 2015-08-14, use reduce_sequence_header for stream control. 348 * v2.0, 2015-08-14, use reduce_sequence_header for stream control.
@@ -43,6 +43,95 @@ using namespace std; @@ -43,6 +43,95 @@ using namespace std;
43 #include <srs_app_source.hpp> 43 #include <srs_app_source.hpp>
44 #include <srs_app_http_conn.hpp> 44 #include <srs_app_http_conn.hpp>
45 45
  46 +int srs_api_response_jsonp(ISrsHttpResponseWriter* w, string callback, string data)
  47 +{
  48 + int ret = ERROR_SUCCESS;
  49 +
  50 + SrsHttpHeader* h = w->header();
  51 +
  52 + h->set_content_length(data.length() + callback.length() + 2);
  53 + h->set_content_type("text/javascript");
  54 +
  55 + if (!callback.empty() && (ret = w->write((char*)callback.data(), (int)callback.length())) != ERROR_SUCCESS) {
  56 + return ret;
  57 + }
  58 +
  59 + static char* c0 = (char*)"(";
  60 + if ((ret = w->write(c0, 1)) != ERROR_SUCCESS) {
  61 + return ret;
  62 + }
  63 + if ((ret = w->write((char*)data.data(), (int)data.length())) != ERROR_SUCCESS) {
  64 + return ret;
  65 + }
  66 +
  67 + static char* c1 = (char*)")";
  68 + if ((ret = w->write(c1, 1)) != ERROR_SUCCESS) {
  69 + return ret;
  70 + }
  71 +
  72 + return ret;
  73 +}
  74 +
  75 +int srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, int code)
  76 +{
  77 + std::stringstream ss;
  78 +
  79 + ss << SRS_JOBJECT_START
  80 + << SRS_JFIELD_ERROR(code)
  81 + << SRS_JOBJECT_END;
  82 +
  83 + return srs_api_response_jsonp(w, callback, ss.str());
  84 +}
  85 +
  86 +int srs_api_response_json(ISrsHttpResponseWriter* w, string data)
  87 +{
  88 + SrsHttpHeader* h = w->header();
  89 +
  90 + h->set_content_length(data.length());
  91 + h->set_content_type("application/json");
  92 +
  93 + return w->write((char*)data.data(), (int)data.length());
  94 +}
  95 +
  96 +int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code)
  97 +{
  98 + std::stringstream ss;
  99 +
  100 + ss << SRS_JOBJECT_START
  101 + << SRS_JFIELD_ERROR(code)
  102 + << SRS_JOBJECT_END;
  103 +
  104 + return srs_api_response_json(w, ss.str());
  105 +}
  106 +
  107 +int srs_api_response(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string json)
  108 +{
  109 + string callback = r->query_get("callback");
  110 + bool jsonp = !callback.empty();
  111 +
  112 + // no jsonp, directly response.
  113 + if (!jsonp) {
  114 + return srs_api_response_json(w, json);
  115 + }
  116 +
  117 + // jsonp, get function name from query("callback")
  118 + return srs_api_response_jsonp(w, callback, json);
  119 +}
  120 +
  121 +int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, int code)
  122 +{
  123 + string callback = r->query_get("callback");
  124 + bool jsonp = !callback.empty();
  125 +
  126 + // no jsonp, directly response.
  127 + if (!jsonp) {
  128 + return srs_api_response_json_code(w, code);
  129 + }
  130 +
  131 + // jsonp, get function name from query("callback")
  132 + return srs_api_response_jsonp_code(w, callback, code);
  133 +}
  134 +
46 SrsGoApiRoot::SrsGoApiRoot() 135 SrsGoApiRoot::SrsGoApiRoot()
47 { 136 {
48 } 137 }
@@ -64,7 +153,7 @@ int SrsGoApiRoot::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -64,7 +153,7 @@ int SrsGoApiRoot::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
64 << SRS_JOBJECT_END 153 << SRS_JOBJECT_END
65 << SRS_JOBJECT_END; 154 << SRS_JOBJECT_END;
66 155
67 - return srs_http_response_json(w, ss.str()); 156 + return srs_api_response(w, r, ss.str());
68 } 157 }
69 158
70 SrsGoApiApi::SrsGoApiApi() 159 SrsGoApiApi::SrsGoApiApi()
@@ -88,7 +177,7 @@ int SrsGoApiApi::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -88,7 +177,7 @@ int SrsGoApiApi::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
88 << SRS_JOBJECT_END 177 << SRS_JOBJECT_END
89 << SRS_JOBJECT_END; 178 << SRS_JOBJECT_END;
90 179
91 - return srs_http_response_json(w, ss.str()); 180 + return srs_api_response(w, r, ss.str());
92 } 181 }
93 182
94 SrsGoApiV1::SrsGoApiV1() 183 SrsGoApiV1::SrsGoApiV1()
@@ -129,7 +218,7 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -129,7 +218,7 @@ int SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
129 << SRS_JOBJECT_END 218 << SRS_JOBJECT_END
130 << SRS_JOBJECT_END; 219 << SRS_JOBJECT_END;
131 220
132 - return srs_http_response_json(w, ss.str()); 221 + return srs_api_response(w, r, ss.str());
133 } 222 }
134 223
135 SrsGoApiVersion::SrsGoApiVersion() 224 SrsGoApiVersion::SrsGoApiVersion()
@@ -156,7 +245,7 @@ int SrsGoApiVersion::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -156,7 +245,7 @@ int SrsGoApiVersion::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
156 << SRS_JOBJECT_END 245 << SRS_JOBJECT_END
157 << SRS_JOBJECT_END; 246 << SRS_JOBJECT_END;
158 247
159 - return srs_http_response_json(w, ss.str()); 248 + return srs_api_response(w, r, ss.str());
160 } 249 }
161 250
162 SrsGoApiSummaries::SrsGoApiSummaries() 251 SrsGoApiSummaries::SrsGoApiSummaries()
@@ -171,7 +260,7 @@ int SrsGoApiSummaries::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -171,7 +260,7 @@ int SrsGoApiSummaries::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
171 { 260 {
172 std::stringstream ss; 261 std::stringstream ss;
173 srs_api_dump_summaries(ss); 262 srs_api_dump_summaries(ss);
174 - return srs_http_response_json(w, ss.str()); 263 + return srs_api_response(w, r, ss.str());
175 } 264 }
176 265
177 SrsGoApiRusages::SrsGoApiRusages() 266 SrsGoApiRusages::SrsGoApiRusages()
@@ -182,39 +271,39 @@ SrsGoApiRusages::~SrsGoApiRusages() @@ -182,39 +271,39 @@ SrsGoApiRusages::~SrsGoApiRusages()
182 { 271 {
183 } 272 }
184 273
185 -int SrsGoApiRusages::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* req) 274 +int SrsGoApiRusages::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
186 { 275 {
187 SrsStatistic* stat = SrsStatistic::instance(); 276 SrsStatistic* stat = SrsStatistic::instance();
188 std::stringstream ss; 277 std::stringstream ss;
189 278
190 - SrsRusage* r = srs_get_system_rusage(); 279 + SrsRusage* ru = srs_get_system_rusage();
191 280
192 ss << SRS_JOBJECT_START 281 ss << SRS_JOBJECT_START
193 << SRS_JFIELD_ERROR(ERROR_SUCCESS) << SRS_JFIELD_CONT 282 << SRS_JFIELD_ERROR(ERROR_SUCCESS) << SRS_JFIELD_CONT
194 << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT 283 << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
195 << SRS_JFIELD_ORG("data", SRS_JOBJECT_START) 284 << SRS_JFIELD_ORG("data", SRS_JOBJECT_START)
196 - << SRS_JFIELD_ORG("ok", (r->ok? "true":"false")) << SRS_JFIELD_CONT  
197 - << SRS_JFIELD_ORG("sample_time", r->sample_time) << SRS_JFIELD_CONT  
198 - << SRS_JFIELD_ORG("ru_utime", r->r.ru_utime.tv_sec) << SRS_JFIELD_CONT  
199 - << SRS_JFIELD_ORG("ru_stime", r->r.ru_stime.tv_sec) << SRS_JFIELD_CONT  
200 - << SRS_JFIELD_ORG("ru_maxrss", r->r.ru_maxrss) << SRS_JFIELD_CONT  
201 - << SRS_JFIELD_ORG("ru_ixrss", r->r.ru_ixrss) << SRS_JFIELD_CONT  
202 - << SRS_JFIELD_ORG("ru_idrss", r->r.ru_idrss) << SRS_JFIELD_CONT  
203 - << SRS_JFIELD_ORG("ru_isrss", r->r.ru_isrss) << SRS_JFIELD_CONT  
204 - << SRS_JFIELD_ORG("ru_minflt", r->r.ru_minflt) << SRS_JFIELD_CONT  
205 - << SRS_JFIELD_ORG("ru_majflt", r->r.ru_majflt) << SRS_JFIELD_CONT  
206 - << SRS_JFIELD_ORG("ru_nswap", r->r.ru_nswap) << SRS_JFIELD_CONT  
207 - << SRS_JFIELD_ORG("ru_inblock", r->r.ru_inblock) << SRS_JFIELD_CONT  
208 - << SRS_JFIELD_ORG("ru_oublock", r->r.ru_oublock) << SRS_JFIELD_CONT  
209 - << SRS_JFIELD_ORG("ru_msgsnd", r->r.ru_msgsnd) << SRS_JFIELD_CONT  
210 - << SRS_JFIELD_ORG("ru_msgrcv", r->r.ru_msgrcv) << SRS_JFIELD_CONT  
211 - << SRS_JFIELD_ORG("ru_nsignals", r->r.ru_nsignals) << SRS_JFIELD_CONT  
212 - << SRS_JFIELD_ORG("ru_nvcsw", r->r.ru_nvcsw) << SRS_JFIELD_CONT  
213 - << SRS_JFIELD_ORG("ru_nivcsw", r->r.ru_nivcsw) 285 + << SRS_JFIELD_ORG("ok", (ru->ok? "true":"false")) << SRS_JFIELD_CONT
  286 + << SRS_JFIELD_ORG("sample_time", ru->sample_time) << SRS_JFIELD_CONT
  287 + << SRS_JFIELD_ORG("ru_utime", ru->r.ru_utime.tv_sec) << SRS_JFIELD_CONT
  288 + << SRS_JFIELD_ORG("ru_stime", ru->r.ru_stime.tv_sec) << SRS_JFIELD_CONT
  289 + << SRS_JFIELD_ORG("ru_maxrss", ru->r.ru_maxrss) << SRS_JFIELD_CONT
  290 + << SRS_JFIELD_ORG("ru_ixrss", ru->r.ru_ixrss) << SRS_JFIELD_CONT
  291 + << SRS_JFIELD_ORG("ru_idrss", ru->r.ru_idrss) << SRS_JFIELD_CONT
  292 + << SRS_JFIELD_ORG("ru_isrss", ru->r.ru_isrss) << SRS_JFIELD_CONT
  293 + << SRS_JFIELD_ORG("ru_minflt", ru->r.ru_minflt) << SRS_JFIELD_CONT
  294 + << SRS_JFIELD_ORG("ru_majflt", ru->r.ru_majflt) << SRS_JFIELD_CONT
  295 + << SRS_JFIELD_ORG("ru_nswap", ru->r.ru_nswap) << SRS_JFIELD_CONT
  296 + << SRS_JFIELD_ORG("ru_inblock", ru->r.ru_inblock) << SRS_JFIELD_CONT
  297 + << SRS_JFIELD_ORG("ru_oublock", ru->r.ru_oublock) << SRS_JFIELD_CONT
  298 + << SRS_JFIELD_ORG("ru_msgsnd", ru->r.ru_msgsnd) << SRS_JFIELD_CONT
  299 + << SRS_JFIELD_ORG("ru_msgrcv", ru->r.ru_msgrcv) << SRS_JFIELD_CONT
  300 + << SRS_JFIELD_ORG("ru_nsignals", ru->r.ru_nsignals) << SRS_JFIELD_CONT
  301 + << SRS_JFIELD_ORG("ru_nvcsw", ru->r.ru_nvcsw) << SRS_JFIELD_CONT
  302 + << SRS_JFIELD_ORG("ru_nivcsw", ru->r.ru_nivcsw)
214 << SRS_JOBJECT_END 303 << SRS_JOBJECT_END
215 << SRS_JOBJECT_END; 304 << SRS_JOBJECT_END;
216 305
217 - return srs_http_response_json(w, ss.str()); 306 + return srs_api_response(w, r, ss.str());
218 } 307 }
219 308
220 SrsGoApiSelfProcStats::SrsGoApiSelfProcStats() 309 SrsGoApiSelfProcStats::SrsGoApiSelfProcStats()
@@ -286,7 +375,7 @@ int SrsGoApiSelfProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage @@ -286,7 +375,7 @@ int SrsGoApiSelfProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
286 << SRS_JOBJECT_END 375 << SRS_JOBJECT_END
287 << SRS_JOBJECT_END; 376 << SRS_JOBJECT_END;
288 377
289 - return srs_http_response_json(w, ss.str()); 378 + return srs_api_response(w, r, ss.str());
290 } 379 }
291 380
292 SrsGoApiSystemProcStats::SrsGoApiSystemProcStats() 381 SrsGoApiSystemProcStats::SrsGoApiSystemProcStats()
@@ -323,7 +412,7 @@ int SrsGoApiSystemProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa @@ -323,7 +412,7 @@ int SrsGoApiSystemProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
323 << SRS_JOBJECT_END 412 << SRS_JOBJECT_END
324 << SRS_JOBJECT_END; 413 << SRS_JOBJECT_END;
325 414
326 - return srs_http_response_json(w, ss.str()); 415 + return srs_api_response(w, r, ss.str());
327 } 416 }
328 417
329 SrsGoApiMemInfos::SrsGoApiMemInfos() 418 SrsGoApiMemInfos::SrsGoApiMemInfos()
@@ -361,7 +450,7 @@ int SrsGoApiMemInfos::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -361,7 +450,7 @@ int SrsGoApiMemInfos::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
361 << SRS_JOBJECT_END 450 << SRS_JOBJECT_END
362 << SRS_JOBJECT_END; 451 << SRS_JOBJECT_END;
363 452
364 - return srs_http_response_json(w, ss.str()); 453 + return srs_api_response(w, r, ss.str());
365 } 454 }
366 455
367 SrsGoApiAuthors::SrsGoApiAuthors() 456 SrsGoApiAuthors::SrsGoApiAuthors()
@@ -390,7 +479,7 @@ int SrsGoApiAuthors::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -390,7 +479,7 @@ int SrsGoApiAuthors::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
390 << SRS_JOBJECT_END 479 << SRS_JOBJECT_END
391 << SRS_JOBJECT_END; 480 << SRS_JOBJECT_END;
392 481
393 - return srs_http_response_json(w, ss.str()); 482 + return srs_api_response(w, r, ss.str());
394 } 483 }
395 484
396 SrsGoApiFeatures::SrsGoApiFeatures() 485 SrsGoApiFeatures::SrsGoApiFeatures()
@@ -516,7 +605,7 @@ int SrsGoApiFeatures::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -516,7 +605,7 @@ int SrsGoApiFeatures::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
516 << SRS_JOBJECT_END 605 << SRS_JOBJECT_END
517 << SRS_JOBJECT_END; 606 << SRS_JOBJECT_END;
518 607
519 - return srs_http_response_json(w, ss.str()); 608 + return srs_api_response(w, r, ss.str());
520 } 609 }
521 610
522 SrsGoApiRequests::SrsGoApiRequests() 611 SrsGoApiRequests::SrsGoApiRequests()
@@ -579,7 +668,7 @@ int SrsGoApiRequests::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -579,7 +668,7 @@ int SrsGoApiRequests::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
579 << SRS_JOBJECT_END 668 << SRS_JOBJECT_END
580 << SRS_JOBJECT_END; 669 << SRS_JOBJECT_END;
581 670
582 - return srs_http_response_json(w, ss.str()); 671 + return srs_api_response(w, r, ss.str());
583 } 672 }
584 673
585 SrsGoApiVhosts::SrsGoApiVhosts() 674 SrsGoApiVhosts::SrsGoApiVhosts()
@@ -605,7 +694,7 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -605,7 +694,7 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
605 if (vid > 0 && (vhost = stat->find_vhost(vid)) == NULL) { 694 if (vid > 0 && (vhost = stat->find_vhost(vid)) == NULL) {
606 ret = ERROR_RTMP_STREAM_NOT_FOUND; 695 ret = ERROR_RTMP_STREAM_NOT_FOUND;
607 srs_error("vhost id=%d not found. ret=%d", vid, ret); 696 srs_error("vhost id=%d not found. ret=%d", vid, ret);
608 - return srs_http_response_code(w, ret); 697 + return srs_api_response_code(w, r, ret);
609 } 698 }
610 699
611 if (r->is_http_get()) { 700 if (r->is_http_get()) {
@@ -629,10 +718,10 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -629,10 +718,10 @@ int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
629 << SRS_JOBJECT_END; 718 << SRS_JOBJECT_END;
630 } 719 }
631 720
632 - return srs_http_response_json(w, ss.str()); 721 + return srs_api_response(w, r, ss.str());
633 } 722 }
634 723
635 - return srs_http_response_json(w, ss.str()); 724 + return srs_api_response(w, r, ss.str());
636 } 725 }
637 726
638 SrsGoApiStreams::SrsGoApiStreams() 727 SrsGoApiStreams::SrsGoApiStreams()
@@ -658,7 +747,7 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -658,7 +747,7 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
658 if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) { 747 if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) {
659 ret = ERROR_RTMP_STREAM_NOT_FOUND; 748 ret = ERROR_RTMP_STREAM_NOT_FOUND;
660 srs_error("stream stream_id=%d not found. ret=%d", sid, ret); 749 srs_error("stream stream_id=%d not found. ret=%d", sid, ret);
661 - return srs_http_response_code(w, ret); 750 + return srs_api_response_code(w, r, ret);
662 } 751 }
663 752
664 if (r->is_http_get()) { 753 if (r->is_http_get()) {
@@ -682,7 +771,7 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -682,7 +771,7 @@ int SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
682 << SRS_JOBJECT_END; 771 << SRS_JOBJECT_END;
683 } 772 }
684 773
685 - return srs_http_response_json(w, ss.str()); 774 + return srs_api_response(w, r, ss.str());
686 } 775 }
687 776
688 return ret; 777 return ret;
@@ -711,7 +800,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -711,7 +800,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
711 if (cid >= 0 && (client = stat->find_client(cid)) == NULL) { 800 if (cid >= 0 && (client = stat->find_client(cid)) == NULL) {
712 ret = ERROR_RTMP_STREAM_NOT_FOUND; 801 ret = ERROR_RTMP_STREAM_NOT_FOUND;
713 srs_error("stream client_id=%d not found. ret=%d", cid, ret); 802 srs_error("stream client_id=%d not found. ret=%d", cid, ret);
714 - return srs_http_response_code(w, ret); 803 + return srs_api_response_code(w, r, ret);
715 } 804 }
716 805
717 if (r->is_http_delete()) { 806 if (r->is_http_delete()) {
@@ -719,7 +808,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -719,7 +808,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
719 808
720 client->conn->expire(); 809 client->conn->expire();
721 srs_warn("delete client=%d ok", cid); 810 srs_warn("delete client=%d ok", cid);
722 - return srs_http_response_code(w, ret); 811 + return srs_api_response_code(w, r, ret);
723 } else if (r->is_http_get()) { 812 } else if (r->is_http_get()) {
724 std::stringstream data; 813 std::stringstream data;
725 814
@@ -741,7 +830,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -741,7 +830,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
741 << SRS_JOBJECT_END; 830 << SRS_JOBJECT_END;
742 } 831 }
743 832
744 - return srs_http_response_json(w, ss.str()); 833 + return srs_api_response(w, r, ss.str());
745 } else { 834 } else {
746 return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed); 835 return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed);
747 } 836 }
@@ -759,7 +848,7 @@ SrsGoApiError::~SrsGoApiError() @@ -759,7 +848,7 @@ SrsGoApiError::~SrsGoApiError()
759 848
760 int SrsGoApiError::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) 849 int SrsGoApiError::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
761 { 850 {
762 - return srs_http_response_code(w, 100); 851 + return srs_api_response_code(w, r, 100);
763 } 852 }
764 853
765 854
@@ -673,6 +673,11 @@ string SrsHttpMessage::path() @@ -673,6 +673,11 @@ string SrsHttpMessage::path()
673 return _uri->get_path(); 673 return _uri->get_path();
674 } 674 }
675 675
  676 +string SrsHttpMessage::query()
  677 +{
  678 + return _uri->get_query();
  679 +}
  680 +
676 string SrsHttpMessage::ext() 681 string SrsHttpMessage::ext()
677 { 682 {
678 return _ext; 683 return _ext;
@@ -245,6 +245,7 @@ public: @@ -245,6 +245,7 @@ public:
245 virtual std::string url(); 245 virtual std::string url();
246 virtual std::string host(); 246 virtual std::string host();
247 virtual std::string path(); 247 virtual std::string path();
  248 + virtual std::string query();
248 virtual std::string ext(); 249 virtual std::string ext();
249 /** 250 /**
250 * get the RESTful matched id. 251 * get the RESTful matched id.
@@ -137,27 +137,6 @@ int srs_go_http_error(ISrsHttpResponseWriter* w, int code, string error) @@ -137,27 +137,6 @@ int srs_go_http_error(ISrsHttpResponseWriter* w, int code, string error)
137 return ret; 137 return ret;
138 } 138 }
139 139
140 -int srs_http_response_json(ISrsHttpResponseWriter* w, string data)  
141 -{  
142 - SrsHttpHeader* h = w->header();  
143 -  
144 - h->set_content_length(data.length());  
145 - h->set_content_type("application/json");  
146 -  
147 - return w->write((char*)data.data(), (int)data.length());  
148 -}  
149 -  
150 -int srs_http_response_code(ISrsHttpResponseWriter* w, int code)  
151 -{  
152 - std::stringstream ss;  
153 -  
154 - ss << SRS_JOBJECT_START  
155 - << SRS_JFIELD_ERROR(code)  
156 - << SRS_JOBJECT_END;  
157 -  
158 - return srs_http_response_json(w, ss.str());  
159 -}  
160 -  
161 SrsHttpHeader::SrsHttpHeader() 140 SrsHttpHeader::SrsHttpHeader()
162 { 141 {
163 } 142 }
@@ -261,17 +240,23 @@ SrsHttpRedirectHandler::~SrsHttpRedirectHandler() @@ -261,17 +240,23 @@ SrsHttpRedirectHandler::~SrsHttpRedirectHandler()
261 int SrsHttpRedirectHandler::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) 240 int SrsHttpRedirectHandler::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
262 { 241 {
263 int ret = ERROR_SUCCESS; 242 int ret = ERROR_SUCCESS;
264 - string msg = "Moved Permsanently"; 243 +
  244 + string location = url;
  245 + if (!r->query().empty()) {
  246 + location += "?" + r->query();
  247 + }
  248 +
  249 + string msg = "Redirect to" + location;
265 250
266 w->header()->set_content_type("text/plain; charset=utf-8"); 251 w->header()->set_content_type("text/plain; charset=utf-8");
267 w->header()->set_content_length(msg.length()); 252 w->header()->set_content_length(msg.length());
268 - w->header()->set("Location", url); 253 + w->header()->set("Location", location);
269 w->write_header(code); 254 w->write_header(code);
270 255
271 w->write((char*)msg.data(), (int)msg.length()); 256 w->write((char*)msg.data(), (int)msg.length());
272 w->final_request(); 257 w->final_request();
273 258
274 - srs_info("redirect to %s.", url.c_str()); 259 + srs_info("redirect to %s.", location.c_str());
275 return ret; 260 return ret;
276 } 261 }
277 262
@@ -636,7 +621,7 @@ int SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler) @@ -636,7 +621,7 @@ int SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler)
636 621
637 entry = new SrsHttpMuxEntry(); 622 entry = new SrsHttpMuxEntry();
638 entry->explicit_match = false; 623 entry->explicit_match = false;
639 - entry->handler = new SrsHttpRedirectHandler(pattern, SRS_CONSTS_HTTP_MovedPermanently); 624 + entry->handler = new SrsHttpRedirectHandler(pattern, SRS_CONSTS_HTTP_Found);
640 entry->pattern = pattern; 625 entry->pattern = pattern;
641 entry->handler->entry = entry; 626 entry->handler->entry = entry;
642 627
@@ -81,14 +81,6 @@ class ISrsHttpResponseWriter; @@ -81,14 +81,6 @@ class ISrsHttpResponseWriter;
81 extern int srs_go_http_error(ISrsHttpResponseWriter* w, int code); 81 extern int srs_go_http_error(ISrsHttpResponseWriter* w, int code);
82 extern int srs_go_http_error(ISrsHttpResponseWriter* w, int code, std::string error); 82 extern int srs_go_http_error(ISrsHttpResponseWriter* w, int code, std::string error);
83 83
84 -// helper function: response in json format.  
85 -extern int srs_http_response_json(ISrsHttpResponseWriter* w, std::string data);  
86 -/**  
87 - * response a typical code object, for example:  
88 - * {code : 100}  
89 - */  
90 -extern int srs_http_response_code(ISrsHttpResponseWriter* w, int code);  
91 -  
92 // get the status text of code. 84 // get the status text of code.
93 extern std::string srs_generate_http_status_text(int status); 85 extern std::string srs_generate_http_status_text(int status);
94 86
@@ -497,6 +489,7 @@ public: @@ -497,6 +489,7 @@ public:
497 virtual std::string url() = 0; 489 virtual std::string url() = 0;
498 virtual std::string host() = 0; 490 virtual std::string host() = 0;
499 virtual std::string path() = 0; 491 virtual std::string path() = 0;
  492 + virtual std::string query() = 0;
500 virtual std::string ext() = 0; 493 virtual std::string ext() = 0;
501 /** 494 /**
502 * get the RESTful id, 495 * get the RESTful id,